Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(improvement) Configuration syntax change #27

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion conf/gsnova.conf
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,16 @@ Proxy=https://GoogleHttps
[C4]
Enable=0
Listen=localhost:48102
WorkerNode[0]=

#--- new style configuration syntax
WorkerNode=xxx.herokuapp.com
WorkerNode=xxx.cloudno.de
WorkerNode=xxx.rhcloud.com

#--- old style configuration syntax
#--- any index from 0 to 9 will be readed, not necessary be successive starting from 0
WorkerNode[8]=

ReadTimeout = 25
MaxConn = 3
WSConnKeepAlive = 1800
Expand Down
6 changes: 3 additions & 3 deletions src/common/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ func InitConfig() error {
// if timeout, exist := Cfg.GetIntProperty("LocalServer", "KeepAliveTimeout"); exist {
// KeepAliveTimeout = time.Duration(timeout)
// }
// if addr, exist := Cfg.GetProperty("LocalProxy", "Proxy"); exist {
// LocalProxy, _ = url.Parse(addr)
// }
// if addr, exist := Cfg.GetProperty("LocalProxy", "Proxy"); exist {
// LocalProxy, _ = url.Parse(addr)
// }
if enable, exist := Cfg.GetIntProperty("Misc", "DebugEnable"); exist {
DebugEnable = (enable != 0)
}
Expand Down
8 changes: 0 additions & 8 deletions src/main/launch.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import (
"remote"
"runtime"
"sync/atomic"
"time"
"util"
)

const (
Expand Down Expand Up @@ -120,12 +118,6 @@ func main() {
if !exist {
log.Fatalln("No config [LocalServer]->Listen found")
}
if v, exist := common.Cfg.GetBoolProperty("Misc", "AutoOpenWebUI"); !exist || v {
go func() {
time.Sleep(1 * time.Second)
util.OpenBrowser("http://localhost:" + common.ProxyPort + "/")
}()
}
testEntry()
startLocalProxyServer(addr, proxy.GLOBAL_PROXY_SERVER)
//launchSystemTray()
Expand Down
29 changes: 24 additions & 5 deletions src/proxy/c4.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import (
"encoding/binary"
"errors"
"event"
//"fmt"
_ "fmt"
"io"
"log"
"net"
"net/http"
"net/url"
_ "os"
"regexp"
"strconv"
"strings"
Expand Down Expand Up @@ -505,7 +506,7 @@ func (manager *C4) Init() error {
}
return url.Parse(c4_cfg.Proxy)
},
ResponseHeaderTimeout: time.Duration(c4_cfg.ReadTimeout + 1) * time.Second,
ResponseHeaderTimeout: time.Duration(c4_cfg.ReadTimeout+1) * time.Second,
}
c4HttpClient.Transport = tr

Expand All @@ -514,11 +515,30 @@ func (manager *C4) Init() error {
go writeCBLoop(i)
}

workers := make([]string, 0)
// new style config syntax
wl, _ := common.Cfg.GetPropertyList("C4", "WorkerNode")
for _, worker := range wl {
workers = append(workers, worker)
}

// old style config syntax
index := 0
for {
v, exist := common.Cfg.GetProperty("C4", "WorkerNode["+strconv.Itoa(index)+"]")
if !exist || len(v) == 0 {
break
// don't exit if there's less than 10 index ...
if index > 9 {
break
}
} else {
workers = append(workers, v)
}
index = index + 1
}
for _, v := range workers {
if len(v) == 0 {
continue
}
if !strings.Contains(v, "://") {
v = "http://" + v
Expand All @@ -527,13 +547,12 @@ func (manager *C4) Init() error {
v = v + "/"
}
manager.servers.Add(v)
index = index + 1
if strings.HasPrefix(v, "ws://") {
initC4WebsocketChannel(v)
}
manager.loginC4(v)
}
if index == 0 {
if len(workers) == 0 {
C4Enable = false
return errors.New("No configed C4 server.")
}
Expand Down
5 changes: 3 additions & 2 deletions src/proxy/local_hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ func loadLocalHostMapping(file string) error {
}
props, exist := ini.GetTagProperties("")
if exist {
for k, v := range props {
for k, _ := range props {
selector := &util.ListSelector{}
hs := strings.Split(v, "|")
vs, _ := ini.GetProperty("", k)
hs := strings.Split(vs, "|")
for _, h := range hs {
selector.Add(h)
}
Expand Down
49 changes: 33 additions & 16 deletions src/util/ini.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,21 @@ package util
import (
"bufio"
"bytes"
_ "fmt"
"io"
"os"
"strconv"
"strings"
)

type Stringlist []string
type Ini struct {
props map[string]map[string]string
props map[string]map[string]Stringlist
}

func NewIni() *Ini {
ini := new(Ini)
ini.props = make(map[string]map[string]string)
ini.props = make(map[string]map[string]Stringlist)
return ini
}

Expand Down Expand Up @@ -51,12 +53,12 @@ func (ini *Ini) Load(is io.Reader) (err error) {
value := strings.TrimSpace(line[idx+1:])
ini.SetProperty(currenttag, key, value)
}
// splits := strings.Split(line, "=")
// if len(splits) >= 2 {
// key := strings.TrimSpace(splits[0])
// value := strings.TrimSpace(splits[1])
// ini.SetProperty(currenttag, key, value)
// }
// splits := strings.Split(line, "=")
// if len(splits) >= 2 {
// key := strings.TrimSpace(splits[0])
// value := strings.TrimSpace(splits[1])
// ini.SetProperty(currenttag, key, value)
// }
}
}
}
Expand All @@ -66,8 +68,10 @@ func (ini *Ini) Load(is io.Reader) (err error) {
func (ini *Ini) Save(os io.Writer) {
if _, ok := ini.props[""]; ok {
for k1, v1 := range ini.props[""] {
line := k1 + " = " + v1 + "\r\n"
os.Write([]byte(line))
for _, v := range v1 {
line := k1 + " = " + v + "\r\n"
os.Write([]byte(line))
}
}
os.Write([]byte("\r\n"))
}
Expand All @@ -76,8 +80,10 @@ func (ini *Ini) Save(os io.Writer) {
k = "[" + k + "]\r\n"
os.Write([]byte(k))
for k1, v1 := range xm {
line := k1 + " = " + v1 + "\r\n"
os.Write([]byte(line))
for _, v := range v1 {
line := k1 + " = " + v + "\r\n"
os.Write([]byte(line))
}
}
os.Write([]byte("\r\n"))
}
Expand All @@ -86,12 +92,12 @@ func (ini *Ini) Save(os io.Writer) {

func (ini *Ini) SetProperty(tag, key, value string) {
if nil == ini.props[tag] {
ini.props[tag] = make(map[string]string)
ini.props[tag] = make(map[string]Stringlist)
}
ini.props[tag][key] = value
ini.props[tag][key] = append(ini.props[tag][key], value)
}

func (ini *Ini) GetProperty(tag, key string) (value string, exist bool) {
func (ini *Ini) GetPropertyList(tag, key string) (value Stringlist, exist bool) {
if m, ok := ini.props[tag]; !ok {
exist = false
return
Expand All @@ -101,6 +107,17 @@ func (ini *Ini) GetProperty(tag, key string) (value string, exist bool) {
return
}

func (ini *Ini) GetProperty(tag, key string) (value string, exist bool) {
var vl Stringlist
vl, exist = ini.GetPropertyList(tag, key)
if len(vl) > 0 {
value = vl[0]
} else {
return "", false
}
return
}

func (ini *Ini) GetIntProperty(tag, key string) (value int64, exist bool) {
var str string
if str, exist = ini.GetProperty(tag, key); exist {
Expand All @@ -125,7 +142,7 @@ func (ini *Ini) GetBoolProperty(tag, key string) (value bool, exist bool) {
return
}

func (ini *Ini) GetTagProperties(tag string) (map[string]string, bool) {
func (ini *Ini) GetTagProperties(tag string) (map[string]Stringlist, bool) {
v, exist := ini.props[tag]
return v, exist
}
Expand Down