Skip to content

Commit

Permalink
Update config
Browse files Browse the repository at this point in the history
  • Loading branch information
hmgle committed Jul 24, 2021
1 parent f7556d0 commit c8a2112
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 3 deletions.
10 changes: 8 additions & 2 deletions local/cmd/graftcp-local/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,14 @@ func parseConfigFile(path string, app *App) error {
if _, err := os.Stat(defaultConf); err == nil {
dlog.Infof("find config: %s", defaultConf)
path = defaultConf
} else {
return nil
} else { // try "/etc/graftcp-local/graftcp-local.conf"
etcConf := "/etc/graftcp-local/graftcp-local.conf"
if _, err := os.Stat(etcConf); err == nil {
dlog.Infof("find config: %s", etcConf)
path = etcConf
} else {
return nil
}
}
}

Expand Down
96 changes: 95 additions & 1 deletion local/cmd/mgraftcp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,15 @@ package main
// int client_main(int argc, char **argv);
import "C"
import (
"bufio"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
"strconv"
"strings"
"syscall"
"unsafe"

Expand Down Expand Up @@ -49,6 +53,7 @@ var (
socks5Addr string = "127.0.0.1:1080"
socks5User string
socks5Pwd string
configFile string

blackIPFile string
whiteIPFile string
Expand All @@ -67,7 +72,8 @@ func init() {
getopt.FlagLong(&socks5Addr, "socks5", 0, "SOCKS5 address")
getopt.FlagLong(&socks5User, "socks5_username", 0, "SOCKS5 username")
getopt.FlagLong(&socks5Pwd, "socks5_password", 0, "SOCKS5 password")
getopt.FlagLong(&enableDebugLog, "enable-debug-log", 0, "enable debug log")
getopt.FlagLong(&enableDebugLog, "enable-debug-log", 0, "Enable debug log")
getopt.FlagLong(&configFile, "config", 0, "Path to the configuration file")

getopt.FlagLong(&blackIPFile, "blackip-file", 'b', "The IP in black-ip-file will connect direct")
getopt.FlagLong(&whiteIPFile, "whiteip-file", 'w', "Only redirect the connect that destination ip in the white-ip-file to SOCKS5")
Expand All @@ -76,6 +82,93 @@ func init() {
getopt.FlagLong(&showVersion, "version", 0, "Print the mgraftcp version information")
}

func setCfg(key, val string) {
switch strings.ToLower(key) {
case "socks5":
socks5Addr = val
case "socks5_username":
socks5User = val
case "socks5_password":
socks5Pwd = val
case "http_proxy":
httpProxyAddr = val
case "select_proxy_mode":
selectProxyMode = val
}
}

func parseLine(line string) (key, val string) {
line = strings.TrimSpace(line)
if strings.HasPrefix(line, "#") {
return
}
items := strings.SplitN(line, "=", 2)
if len(items) < 2 {
return "", ""
}
return strings.TrimSpace(items[0]), strings.TrimSpace(items[1])
}

func parseConfigFile(path string) error {
if path == "" {
// try default config file "graftcp-local.conf"
exePath := local.GetExePath()
defaultConf := filepath.Dir(exePath) + "/graftcp-local.conf"
if _, err := os.Stat(defaultConf); err == nil {
if enableDebugLog {
dlog.Infof("find config: %s", defaultConf)
}
path = defaultConf
} else { // try "/etc/graftcp-local/graftcp-local.conf"
etcConf := "/etc/graftcp-local/graftcp-local.conf"
if _, err := os.Stat(etcConf); err == nil {
if enableDebugLog {
dlog.Infof("find config: %s", etcConf)
}
path = etcConf
} else {
return nil
}
}
}

file, err := os.Open(path)
if err != nil {
if enableDebugLog {
dlog.Errorf("os.Open(%s) err: %s", path, err.Error())
}
return err
}
defer file.Close()

flagset := make(map[string]bool)
getopt.Getopt(func(opt getopt.Option) bool {
flagset[opt.LongName()] = true
return true
})
reader := bufio.NewReader(file)
for {
line, err := reader.ReadString('\n')
if err != nil {
if err == io.EOF {
if k, v := parseLine(line); !flagset[k] {
setCfg(k, v)
}
break
}
if enableDebugLog {
dlog.Errorf("reader.ReadString('\\n') err: %s, path: %s", err.Error(), path)
}
return err
}
if k, v := parseLine(line); !flagset[k] {
setCfg(k, v)
}
}

return nil
}

func main() {
getopt.Parse()
if showVersion {
Expand All @@ -87,6 +180,7 @@ func main() {
getopt.Usage()
return
}
parseConfigFile(configFile)

retCode := 0
defer func() { os.Exit(retCode) }()
Expand Down

0 comments on commit c8a2112

Please sign in to comment.