Skip to content

Commit

Permalink
Optimizing compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
刘河 committed Mar 30, 2019
1 parent 2b841ad commit 5bbf247
Show file tree
Hide file tree
Showing 9 changed files with 142 additions and 193 deletions.
9 changes: 6 additions & 3 deletions bridge/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func (s *Bridge) cliProcess(c *conn.Conn) {
}
//write server version to client
c.Write([]byte(crypt.Md5(version.GetVersion())))
c.SetReadDeadlineByType(5, s.tunnelType)
c.SetReadDeadlineBySecond(5)
var buf []byte
var err error
//get vKey from client
Expand Down Expand Up @@ -318,9 +318,12 @@ func (s *Bridge) SendLinkInfo(clientId int, link *conn.Link, linkAddr string, t
return
}
if t != nil && t.Mode == "file" {
//TODO if t.mode is file ,not use crypt or compress
link.Crypt = false
link.Compress = false
return
}
if _, err = conn.NewConn(target).SendLinkInfo(link); err != nil {
if _, err = conn.NewConn(target).SendInfo(link, ""); err != nil {
logs.Info("new connect error ,the target %s refuse to connect", link.Host)
return
}
Expand Down Expand Up @@ -445,7 +448,7 @@ loop:
fail = true
c.WriteAddFail()
break loop
} else if t.Mode == "secret" {
} else if t.Mode == "secret" || t.Mode == "p2p" {
ports = append(ports, 0)
}
if len(ports) == 0 {
Expand Down
18 changes: 11 additions & 7 deletions client/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func StartFromFile(path string) {
first := true
cnf, err := config.NewConfig(path)
if err != nil || cnf.CommonConfig == nil {
logs.Error("Config file %s loading error", path)
logs.Error("Config file %s loading error %s", path, err.Error())
os.Exit(0)
}
logs.Info("Loading configuration file %s successfully", path)
Expand Down Expand Up @@ -115,12 +115,12 @@ re:
vkey := cnf.CommonConfig.VKey
if isPub {
// send global configuration to server and get status of config setting
if _, err := c.SendConfigInfo(cnf.CommonConfig); err != nil {
if _, err := c.SendInfo(cnf.CommonConfig.Client, common.NEW_CONF); err != nil {
logs.Error(err)
goto re
}
if !c.GetAddStatus() {
logs.Error(errAdd)
logs.Error("the web_user may have been occupied!")
goto re
}

Expand All @@ -134,7 +134,7 @@ re:

//send hosts to server
for _, v := range cnf.Hosts {
if _, err := c.SendHostInfo(v); err != nil {
if _, err := c.SendInfo(v, common.NEW_HOST); err != nil {
logs.Error(err)
goto re
}
Expand All @@ -146,12 +146,12 @@ re:

//send task to server
for _, v := range cnf.Tasks {
if _, err := c.SendTaskInfo(v); err != nil {
if _, err := c.SendInfo(v, common.NEW_TASK); err != nil {
logs.Error(err)
goto re
}
if !c.GetAddStatus() {
logs.Error(errAdd, v.Ports)
logs.Error(errAdd, v.Ports, v.Remark)
goto re
}
if v.Mode == "file" {
Expand All @@ -166,7 +166,11 @@ re:
}

c.Close()
logs.Notice("web access login key ", vkey)
if cnf.CommonConfig.Client.WebUserName == "" || cnf.CommonConfig.Client.WebPassword == "" {
logs.Notice("web access login username:user password:%s", vkey)
} else {
logs.Notice("web access login username:%s password:%s", cnf.CommonConfig.Client.WebUserName, cnf.CommonConfig.Client.WebPassword)
}
NewRPClient(cnf.CommonConfig.Server, vkey, cnf.CommonConfig.Tp, cnf.CommonConfig.ProxyUrl, cnf).Start()
CloseLocalServer()
goto re
Expand Down
7 changes: 4 additions & 3 deletions client/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,13 @@ func processP2P(localTcpConn net.Conn, config *config.CommonConfig, l *config.Lo
logs.Error(err)
return
}
link := conn.NewLink(common.CONN_TCP, l.Target, config.Cnf.Crypt, config.Cnf.Compress, localTcpConn.LocalAddr().String())
if _, err := conn.NewConn(nowConn).SendLinkInfo(link); err != nil {
//TODO just support compress now because there is not tls file in client packages
link := conn.NewLink(common.CONN_TCP, l.Target, false, config.Client.Cnf.Compress, localTcpConn.LocalAddr().String())
if _, err := conn.NewConn(nowConn).SendInfo(link, ""); err != nil {
logs.Error(err)
return
}
conn.CopyWaitGroup(nowConn, localTcpConn, config.Cnf.Crypt, config.Cnf.Compress, nil, nil, false, nil)
conn.CopyWaitGroup(nowConn, localTcpConn, false, config.Client.Cnf.Compress, nil, nil, false, nil)
}

func newUdpConn(config *config.CommonConfig, l *config.LocalServer) {
Expand Down
44 changes: 31 additions & 13 deletions conf/npc.conf
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@ server_addr=127.0.0.1:8024
conn_type=tcp
vkey=123
auto_reconnection=true
max_conn=1000
flow_limit=1000
rate_limit=1000
basic_username=11
basic_password=3
web_username=user
web_password=1234
crypt=true
compress=true

[health_check_test1]
health_check_timeout=1
Expand All @@ -18,9 +27,10 @@ health_check_max_failed=3
health_check_interval=1
health_check_type=tcp
health_check_target=127.0.0.1:8083,127.0.0.1:8082

[web]
host=b.o.com
target_addr=127.0.0.1:8080,127.0.0.1:8082
host=c.o.com
target_addr=127.0.0.1:8083,127.0.0.1:8082

[tcp]
mode=tcp
Expand All @@ -29,29 +39,37 @@ server_port=10000

[socks5]
mode=socks5
server_port=9005

[http]
mode=httpProxy
server_port=9004

server_port=19009

[file]
mode=file
server_port=9009
server_port=19008
local_path=./
strip_pre=/web/

[http]
mode=httpProxy
server_port=19004

[udp]
mode=udp
server_port=53
server_port=12253
target_addr=114.114.114.114:53

[ssh_secret]
mode=secret
password=ssh2
target_addr=123.206.77.88:22

[ssh_p2p]
mode=p2p
password=ssh3

[secret_ssh]
local_port=2001
password=sec
password=ssh2

[p2p_ssh]
local_port=2002
password=ppp
target_addr=192.168.74.199:22
password=ssh3
target_addr=123.206.77.88:22
22 changes: 13 additions & 9 deletions lib/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package config

import (
"errors"
"fmt"
"github.com/cnlh/nps/lib/common"
"github.com/cnlh/nps/lib/file"
"regexp"
Expand All @@ -13,7 +14,6 @@ type CommonConfig struct {
VKey string
Tp string //bridgeType kcp or tcp
AutoReconnection bool
Cnf *file.Config
ProxyUrl string
Client *file.Client
}
Expand Down Expand Up @@ -104,8 +104,8 @@ func getTitleContent(s string) string {

func dealCommon(s string) *CommonConfig {
c := &CommonConfig{}
c.Cnf = new(file.Config)
c.Client = file.NewClient("", true, true)
c.Client.Cnf = new(file.Config)
for _, v := range splitStr(s) {
item := strings.Split(v, "=")
if len(item) == 0 {
Expand All @@ -122,14 +122,18 @@ func dealCommon(s string) *CommonConfig {
c.Tp = item[1]
case "auto_reconnection":
c.AutoReconnection = common.GetBoolByStr(item[1])
case "username":
c.Cnf.U = item[1]
case "password":
c.Cnf.P = item[1]
case "basic_username":
c.Client.Cnf.U = item[1]
case "basic_password":
c.Client.Cnf.P = item[1]
case "web_password":
c.Client.WebPassword = item[1]
case "web_username":
c.Client.WebUserName = item[1]
case "compress":
c.Cnf.Compress = common.GetBoolByStr(item[1])
c.Client.Cnf.Compress = common.GetBoolByStr(item[1])
case "crypt":
c.Cnf.Crypt = common.GetBoolByStr(item[1])
c.Client.Cnf.Crypt = common.GetBoolByStr(item[1])
case "proxy_url":
c.ProxyUrl = item[1]
case "rate_limit":
Expand Down Expand Up @@ -270,7 +274,7 @@ func getAllTitle(content string) (arr []string, err error) {
m := make(map[string]bool)
for _, v := range arr {
if _, ok := m[v]; ok {
err = errors.New("Item names are not allowed to be duplicated")
err = errors.New(fmt.Sprintf("Item names %s are not allowed to be duplicated", v))
return
}
m[v] = true
Expand Down
Loading

0 comments on commit 5bbf247

Please sign in to comment.