Skip to content

Commit

Permalink
finish config reading of server side
Browse files Browse the repository at this point in the history
  • Loading branch information
asche910 committed Oct 19, 2019
1 parent eff2c4f commit cf587df
Show file tree
Hide file tree
Showing 9 changed files with 175 additions and 60 deletions.
7 changes: 2 additions & 5 deletions client/flyclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ type FlyClient struct {
//protocol string // tcp or udp protocol
ServerAddr string
PACMode bool
Verbose bool
LogPath string

}

func (client *FlyClient) LocalSocks5Proxy(port string) {
Expand All @@ -24,8 +21,8 @@ func (client *FlyClient) LocalHttpProxy(port string) {
fly.StartHttp(port)
}

func (client *FlyClient) Socks5ProxyForTCP(localPort, serverAddr, method, key string) {
fly.Socks5ForClientByTCP(localPort, serverAddr, method, key)
func (client *FlyClient) Socks5ProxyForTCP(localPort, serverAddr, method, key string, pacMode bool) {
fly.Socks5ForClientByTCP(localPort, serverAddr, method, key, pacMode)
}

func (client *FlyClient) Socks5ProxyForUDP(localPort, serverAddr string) {
Expand Down
64 changes: 43 additions & 21 deletions cmd/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func main() {
case 2:
flyClient.LocalSocks5Proxy(flyClient.Ports[0])
case 3:
flyClient.Socks5ProxyForTCP(flyClient.Ports[0], flyClient.ServerAddr, flyClient.Method, flyClient.Password)
flyClient.Socks5ProxyForTCP(flyClient.Ports[0], flyClient.ServerAddr, flyClient.Method, flyClient.Password, flyClient.PACMode)
case 4:
flyClient.Socks5ProxyForUDP(flyClient.Ports[0], flyClient.ServerAddr)
case 5:
Expand All @@ -51,8 +51,11 @@ func printHelp() {
'aes-256-cfb', 'aes-128-ctr', 'aes-192-ctr', 'aes-256-ctr', 'rc4-md5',
'rc4-md5-6', 'chacha20', 'chacha20-ietf'], default is 'aes-256-cfb'
-P, --password password of server
-p, --pac having this flag, pac-mode will open
-C, --config read from config file
-V, --verbose output detail info
-l, --log output detail info to log file
-l, --log output detail info to log file, which if not indicated "flynet.log" will
be default
-H, --help show detail usage
Mail bug reports and suggestions to <asche910@gmail.com>
Expand Down Expand Up @@ -89,9 +92,9 @@ func parseArgs(args []string) {
os.Exit(1)
}
case "--listen", "-L":
if len(args) > 1 && !strings.HasPrefix(args[1], "-") {
if hasNAttributes(args, 1) {
fly.CheckPort(args[1])
if len(args) > 2 && !strings.HasPrefix(args[2], "-") {
if hasNAttributes(args, 2) {
fly.CheckPort(args[2])
flyClient.Ports = []string{args[1], args[2]}
parseArgs(args[3:])
Expand All @@ -105,16 +108,16 @@ func parseArgs(args []string) {
os.Exit(1)
}
case "--server", "-S":
if len(args) > 1 && !strings.HasPrefix(args[1], "-") {
if hasNAttributes(args, 1) {
flyClient.ServerAddr = args[1]
parseArgs(args[2:])
} else {
fmt.Println("flynet: no correct serverAddr found!")
printHelp()
os.Exit(1)
}
case "-m", "--method":
if len(args) > 1 && !strings.HasPrefix(args[1], "-") {
case "--method", "-m":
if hasNAttributes(args, 1) {
flyClient.Method = args[1]
parseArgs(args[2:])
} else {
Expand All @@ -123,20 +126,31 @@ func parseArgs(args []string) {
os.Exit(1)
}
case "--password", "-P":
if len(args) > 1 && !strings.HasPrefix(args[1], "-") {
if hasNAttributes(args, 1) {
flyClient.Password = args[1]
parseArgs(args[2:])
} else {
fmt.Println("flynet: no password found!")
printHelp()
os.Exit(1)
}
case "--pac", "-p":
flyClient.PACMode = true
parseArgs(args[1:])
case "--config", "-C":
if hasNAttributes(args, 1) {
readConfig(args[1])
} else {
fmt.Println("flynet: no config file found!")
printHelp()
os.Exit(1)
}
case "--verbose", "-V":
fly.EnableDebug(true)
parseArgs(args[1:])
case "--logs", "-l":
if len(args) > 1 && !strings.HasPrefix(args[1], "-") {
flyClient.LogPath = args[1]
fly.SetLogName(args[1])
}
fly.EnableLog(true)
parseArgs(args[1:])
Expand All @@ -151,6 +165,22 @@ func parseArgs(args []string) {
}
}

// judge if the current args option has n attributes at least.
// for example:
// args = []string{"--listen", "1080", "8888"}
// hasNAttributes(args, 2) will return true
func hasNAttributes(args []string, n int) bool {
if len(args) > n {
for i := 1; i <= n; i++ {
if strings.HasPrefix(args[i], "-") {
return false
}
}
return true
}
return false
}

func checkArgs() {
mode := flyClient.Mode
if mode == 0 {
Expand All @@ -177,7 +207,7 @@ func readConfig(path string) {
conf, err := ini.Load(path)
if err != nil {
fmt.Println("load config file fail --->", err)
fmt.Println("you could refer the example config file: https://github.com/asche910/flynet")
fmt.Println("you could refer to the example config file: https://github.com/asche910/flynet")
os.Exit(1)
}
section, err := conf.NewSection("client")
Expand All @@ -189,7 +219,7 @@ func readConfig(path string) {
serverAddr := getAttr(section, "serverAddr")
method := getAttr(section, "method")
password := getAttr(section, "password")
pacOn := getAttr(section, "pac-on")
pacOn := getAttr(section, "pac-mode")
verbose := getAttr(section, "verbose")
logs := getAttr(section, "log")

Expand All @@ -212,28 +242,20 @@ func readConfig(path string) {

fly.CheckPort(port)
flyClient.Ports = []string{port}

flyClient.ServerAddr = serverAddr

flyClient.Method = method

flyClient.Password = password

if pacOn == "true" {
flyClient.PACMode = true
}else {
flyClient.PACMode = false
flyClient.PACMode = true
}

if verbose == "true" {
flyClient.Verbose = true
fly.EnableDebug(true)
}else {
flyClient.Verbose = false
}

if logs != "" {
flyClient.LogPath = logs
fly.SetLogName(logs)
fly.EnableLog(true)
}
}
Expand Down
109 changes: 100 additions & 9 deletions cmd/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"github.com/asche910/flynet/fly"
"github.com/asche910/flynet/server"
"gopkg.in/ini.v1"
"os"
"strings"
)
Expand Down Expand Up @@ -48,9 +49,11 @@ func printHelp() {
-m, --method choose a encrypt method, which must be one of ['aes-128-cfb','aes-192-cfb',
'aes-256-cfb', 'aes-128-ctr', 'aes-192-ctr', 'aes-256-ctr', 'rc4-md5',
'rc4-md5-6', 'chacha20', 'chacha20-ietf'], default is 'aes-256-cfb'
-P, --password password for client connecting
-P, --password password for client to connect
-C, --config read from config file
-V, --verbose output detail info
-l, --log output detail info to log file
-l, --log output detail info to log file, which if not indicated "flynet.log" will
be default
-H, --help show detail usage
Mail bug reports and suggestions to <asche910@gmail.com>
Expand Down Expand Up @@ -87,9 +90,9 @@ func parseArgs(args []string) {
os.Exit(1)
}
case "--listen", "-L":
if len(args) > 1 && !strings.HasPrefix(args[1], "-") {
if hasNAttributes(args, 1) {
fly.CheckPort(args[1])
if len(args) > 2 && !strings.HasPrefix(args[2], "-") {
if hasNAttributes(args, 2) {
fly.CheckPort(args[2])
flyServer.Ports = []string{args[1], args[2]}
parseArgs(args[3:])
Expand All @@ -103,7 +106,7 @@ func parseArgs(args []string) {
os.Exit(1)
}
case "-m", "--method":
if len(args) > 1 && !strings.HasPrefix(args[1], "-") {
if hasNAttributes(args, 1) {
flyServer.Method = args[1]
parseArgs(args[2:])
} else {
Expand All @@ -112,20 +115,28 @@ func parseArgs(args []string) {
os.Exit(1)
}
case "--password", "-P":
if len(args) > 1 && !strings.HasPrefix(args[1], "-") {
if hasNAttributes(args, 1) {
flyServer.Password = args[1]
parseArgs(args[2:])
} else {
fmt.Println("flynet: no password found!")
printHelp()
os.Exit(1)
}
case "--config", "-C":
if hasNAttributes(args, 1) {
readConfig(args[1])
} else {
fmt.Println("flynet: no config file found!")
printHelp()
os.Exit(1)
}
case "--verbose", "-V":
fly.EnableDebug(true)
parseArgs(args[1:])
case "--logs", "-l":
if len(args) > 1 && !strings.HasPrefix(args[1], "-") {
flyServer.LogPath = args[1]
if hasNAttributes(args, 1) {
fly.SetLogName(args[1])
}
fly.EnableLog(true)
parseArgs(args[1:])
Expand All @@ -140,6 +151,22 @@ func parseArgs(args []string) {
}
}

// judge if the current args option has n attributes at least.
// for example:
// args = []string{"--listen", "1080", "8888"}
// hasNAttributes(args, 2) will return true
func hasNAttributes(args []string, n int) bool {
if len(args) > n {
for i := 1; i <= n; i++ {
if strings.HasPrefix(args[i], "-") {
return false
}
}
return true
}
return false
}

func checkArgs() {
if flyServer.Mode == 0 {
fmt.Println("Please choose a mode!")
Expand All @@ -160,8 +187,72 @@ func checkArgs() {
}
}

func readConfig() {
// load config file
func readConfig(path string) {
conf, err := ini.Load(path)
if err != nil {
fmt.Println("load config file fail --->", err)
fmt.Println("you could refer to the example config file: https://github.com/asche910/flynet")
os.Exit(1)
}
section, err := conf.NewSection("server")
if err != nil {
fmt.Println("read client tag failed --->", err)
}
mode := getAttr(section, "mode")
portStr := getAttr(section, "port")
method := getAttr(section, "method")
password := getAttr(section, "password")
verbose := getAttr(section, "verbose")
logs := getAttr(section, "log")

switch mode {
case ModeMap[1]:
flyServer.Mode = 1
case ModeMap[2], "socks":
flyServer.Mode = 2
case ModeMap[3], "socks-tcp":
flyServer.Mode = 3
case ModeMap[4], "socks-udp":
flyServer.Mode = 4
case ModeMap[5]:
flyServer.Mode = 5
default:
fmt.Println("flynet: no correct mode found!")
printHelp()
os.Exit(1)
}

// in case of forward mode, which needs two port
ports := strings.Fields(portStr)
if len(ports) == 2 {
fly.CheckPort(ports[0])
fly.CheckPort(ports[1])
flyServer.Ports = []string{ports[0], ports[1]}
}else if len(ports) == 1 {
fly.CheckPort(ports[0])
flyServer.Ports = []string{ports[0]}
}
flyServer.Method = method
flyServer.Password = password

if verbose == "true" {
fly.EnableDebug(true)
}

if logs != "" {
fly.SetLogName(logs)
fly.EnableLog(true)
}
}

// read attr from config file
func getAttr(section *ini.Section, name string) string {
key, e := section.GetKey(name)
if e != nil {
return ""
}
return key.String()
}

func initLog() {
Expand Down
5 changes: 0 additions & 5 deletions fly/config.go

This file was deleted.

10 changes: 8 additions & 2 deletions fly/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ var (
logFlag = false
debugFlag = false
logger *log.Logger
logName = "flynet.log"
)

func InitLog() {
Expand All @@ -24,6 +25,11 @@ func EnableDebug(flag bool) {
debugFlag = flag
}

// set log file name, which can include absolute path
func SetLogName(name string) {
logName = name
}

// return a logger
func GetLogger() *log.Logger {
if logger != nil {
Expand All @@ -32,9 +38,9 @@ func GetLogger() *log.Logger {
var f *os.File
var err error
if logFlag {
f, err = os.OpenFile("flynet.log", os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
f, err = os.OpenFile(logName, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
if err != nil {
log.Println("Create logger file failed!", err)
log.Println("create or open log file failed --->", err)
}
}
var writers []io.Writer
Expand Down
Loading

0 comments on commit cf587df

Please sign in to comment.