Skip to content

Commit

Permalink
gq-server can now accept semi-colon seperated plugin-opts
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeuw committed Sep 20, 2018
1 parent 63d1a6c commit 8e580ff
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
8 changes: 4 additions & 4 deletions cmd/gq-server/gq-server.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,18 +209,18 @@ func main() {
var remoteHost string
// Outbound listening ip, should be 443
var remotePort string
var configPath string
var pluginOpts string
if os.Getenv("SS_LOCAL_HOST") != "" {
localHost = os.Getenv("SS_LOCAL_HOST")
localPort = os.Getenv("SS_LOCAL_PORT")
remoteHost = os.Getenv("SS_REMOTE_HOST")
remotePort = os.Getenv("SS_REMOTE_PORT")
configPath = os.Getenv("SS_PLUGIN_OPTIONS")
pluginOpts = os.Getenv("SS_PLUGIN_OPTIONS")
} else {
localAddr := flag.String("r", "", "localAddr: 127.0.0.1:server_port as set in SS config")
flag.StringVar(&remoteHost, "s", "0.0.0.0", "remoteHost: outbound listing ip, set to 0.0.0.0 to listen to everything")
flag.StringVar(&remotePort, "p", "443", "remotePort: outbound listing port, should be 443")
flag.StringVar(&configPath, "c", "gqserver.json", "configPath: path to gqserver.json")
flag.StringVar(&pluginOpts, "c", "gqserver.json", "pluginOpts: path to gqserver.json or options seperated by semicolons")
askVersion := flag.Bool("v", false, "Print the version number")
printUsage := flag.Bool("h", false, "Print this message")
flag.Parse()
Expand Down Expand Up @@ -250,7 +250,7 @@ func main() {
Now: time.Now,
UsedRandom: map[[32]byte]int{},
}
err := sta.ParseConfig(configPath)
err := sta.ParseConfig(pluginOpts)
if err != nil {
log.Fatalf("Configuration file error: %v", err)
}
Expand Down
41 changes: 36 additions & 5 deletions gqserver/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"crypto/sha256"
"encoding/json"
"io/ioutil"
"strings"
"sync"
"time"
)
Expand All @@ -29,11 +30,41 @@ type State struct {
UsedRandom map[[32]byte]int
}

// ParseConfig parses the config file into a State variable
func (sta *State) ParseConfig(configPath string) error {
content, err := ioutil.ReadFile(configPath)
if err != nil {
return err
// semi-colon separated value.
func ssvToJson(ssv string) (ret []byte) {
unescape := func(s string) string {
r := strings.Replace(s, "\\\\", "\\", -1)
r = strings.Replace(r, "\\=", "=", -1)
r = strings.Replace(r, "\\;", ";", -1)
return r
}
lines := strings.Split(unescape(ssv), ";")
ret = []byte("{")
for _, ln := range lines {
if ln == "" {
break
}
sp := strings.SplitN(ln, "=", 2)
key := sp[0]
value := sp[1]
ret = append(ret, []byte("\""+key+"\":\""+value+"\",")...)

}
ret = ret[:len(ret)-1] // remove the last comma
ret = append(ret, '}')
return ret
}

// ParseConfig parses the config (either a path to json or in-line ssv config) into a State variable
func (sta *State) ParseConfig(config string) (err error) {
var content []byte
if strings.Contains(config, ";") && strings.Contains(config, "=") {
content = ssvToJson(config)
} else {
content, err = ioutil.ReadFile(config)
if err != nil {
return err
}
}
err = json.Unmarshal(content, &sta)
if err != nil {
Expand Down

0 comments on commit 8e580ff

Please sign in to comment.