Skip to content

Commit

Permalink
Code optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
刘河 committed Mar 23, 2019
1 parent efa341c commit b189fb1
Show file tree
Hide file tree
Showing 260 changed files with 50,741 additions and 846 deletions.
235 changes: 93 additions & 142 deletions bridge/bridge.go

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,6 @@ retry:
s.processor(c)
}

func (s *TRPClient) Close() {
s.tunnel.Close()
s.signal.Close()
s.ticker.Stop()
}

//处理
func (s *TRPClient) processor(c *conn.Conn) {
s.signal = c
Expand Down Expand Up @@ -232,3 +226,9 @@ loop:
}
}
}

func (s *TRPClient) Close() {
s.tunnel.Close()
s.signal.Close()
s.ticker.Stop()
}
2 changes: 0 additions & 2 deletions client/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,9 @@ func GetTaskStatus(path string) {
if err != nil {
log.Fatalln(err)
}

if _, err := c.Write([]byte(common.WORK_STATUS)); err != nil {
log.Fatalln(err)
}

//read now vKey and write to server
if f, err := common.ReadAllFromFile(filepath.Join(common.GetTmpPath(), "npc_vkey.txt")); err != nil {
log.Fatalln(err)
Expand Down
17 changes: 4 additions & 13 deletions client/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/cnlh/nps/vender/github.com/xtaci/kcp"
"net"
"net/http"
"strings"
)

var LocalServer []*net.TCPListener
Expand Down Expand Up @@ -51,21 +50,13 @@ func StartLocalServer(l *config.LocalServer, config *config.CommonConfig) error
}
LocalServer = append(LocalServer, listener)
logs.Info("successful start-up of local monitoring, port", l.Port)
for {
c, err := listener.AcceptTCP()
if err != nil {
if strings.Contains(err.Error(), "use of closed network connection") {
break
}
logs.Info(err)
continue
}
conn.Accept(listener, func(c net.Conn) {
if l.Type == "secret" {
go processSecret(c, config, l)
processSecret(c, config, l)
} else {
go processP2P(c, config, l)
processP2P(c, config, l)
}
}
})
return nil
}

Expand Down
4 changes: 4 additions & 0 deletions cmd/nps/nps.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package main
import (
"flag"
"github.com/cnlh/nps/lib/common"
"github.com/cnlh/nps/lib/crypt"
"github.com/cnlh/nps/lib/daemon"
"github.com/cnlh/nps/lib/file"
"github.com/cnlh/nps/lib/install"
"github.com/cnlh/nps/lib/version"
"github.com/cnlh/nps/server"
"github.com/cnlh/nps/server/connection"
"github.com/cnlh/nps/server/test"
"github.com/cnlh/nps/server/tool"
"github.com/cnlh/nps/vender/github.com/astaxie/beego"
"github.com/cnlh/nps/vender/github.com/astaxie/beego/logs"
_ "github.com/cnlh/nps/web/routers"
Expand Down Expand Up @@ -60,5 +62,7 @@ func main() {
}
logs.Info("the version of server is %s ,allow client version to be %s", version.VERSION, version.GetVersion())
connection.InitConnectionService()
crypt.InitTls(filepath.Join(beego.AppPath, "conf", "server.pem"), filepath.Join(beego.AppPath, "conf", "server.key"))
tool.InitAllowPort()
server.StartNewServer(bridgePort, task, beego.AppConfig.String("bridge_type"))
}
6 changes: 4 additions & 2 deletions conf/clients.csv
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
2,corjmrbhr33otit1,,true,,,0,false,0,0,0
5,2dyy78gj7b9zw09l,,true,,,1,false,0,0,0
8,sl6ytyj7lv5224zr,,true,,,0,false,0,0,0,0
2,sqk6hfw7qirdmuep,,true,,,1,false,0,0,0,0
11,1hh13x99zrw90lg7,,true,,,0,false,0,0,0,1
5,phmgbeyns2yvtl6m,,true,,,0,false,0,0,0,1
6 changes: 3 additions & 3 deletions conf/hosts.csv
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
c.o.com,10.1.50.196:4000,5,,,,/,2,7543392,22379,all
a.o.com,127.0.0.1:8080,2,,,,/,3,0,0,all
b.o.com,127.0.0.1:8082,5,,,,/,4,0,0,all
a.o.com,127.0.0.1:8080,2,,,,/,1,0,60307,all
c.o.com,127.0.0.1:8082,2,,,,/,2,0,0,http
c.o.com,127.0.0.1:8082,2,,,,/,3,0,0,https
27 changes: 10 additions & 17 deletions conf/npc.conf
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
[common]
server=127.0.0.1:8024
tp=tcp
vkey=2dyy78gj7b9zw09l
vkey=123
auto_reconnection=true
[web]
host=b.o.com
target=10.1.50.203:80

[health_check_test1]
health_check_timeout=1
Expand All @@ -21,7 +18,9 @@ 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=127.0.0.1:8080

[tcp]
mode=tcp
Expand All @@ -40,26 +39,20 @@ port=9004

[file]
mode=file
port=9100
port=9009
local_path=./
strip_pre=/web/

[s_ssh]
mode=secret
password=1234
target=123.206.77.88:22

[udp]
mode=udp
port=53
target=114.114.114.114:53

[p2p_ssh]
[secret_ssh]
port=2001
password=p2pssh
target=123.206.77.88:22

password=sec

[secret_ssh]
[p2p_ssh]
port=2002
password=1234
password=c
target=123.206.77.88:22
13 changes: 6 additions & 7 deletions conf/nps.conf
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
appname = nps
#Boot mode(dev|pro)
runmode = dev


runmode = pro

#HTTP(S) proxy port, no startup if empty
http_proxy_port=80
#https_proxy_port=443
https_proxy_port=443
https_just_proxy=true
#certFile absolute path
pem_path=conf/server.pem
#pem_path=conf/server.pem
#KeyFile absolute path
key_path=conf/server.key
#key_path=conf/server.key

##bridge
bridge_type=tcp
Expand Down Expand Up @@ -42,7 +40,8 @@ web_username=admin
web_password=123
web_port = 8080
web_ip=0.0.0.0
#Web API unauthenticated IP address

#Web API unauthenticated IP address(the len of auth_crypt_key must be 16)
auth_key=test
auth_crypt_key =1234567812345678

Expand Down
8 changes: 3 additions & 5 deletions conf/tasks.csv
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
8025,socks5,,1,1,2,,0,0,
8026,httpProxy,,1,2,2,,0,0,
9002,tcp,127.0.0.1:8082,1,3,2,,0,0,
9003,socks5,,1,5,5,,0,0,
9009,tcp,127.0.0.1:8082,1,21,5,,8244480,2382592,
0,p2p,,1,25,5,,0,0,p2ptest
0,p2p,,1,1,2,,0,0,c
0,secret,123.206.77.88:22,1,11,2,sec,0,0,sec
11 changes: 11 additions & 0 deletions lib/common/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ import (
"net/http"
"os"
"regexp"
"sort"
"strconv"
"strings"
"sync"
)

//Get the corresponding IP address through domain name
Expand Down Expand Up @@ -344,3 +346,12 @@ func BytesToNum(b []byte) int {
x, _ := strconv.Atoi(str)
return int(x)
}

func GetMapKeys(m sync.Map) (keys []int) {
m.Range(func(key, value interface{}) bool {
keys = append(keys, key.(int))
return true
})
sort.Ints(keys)
return
}
7 changes: 6 additions & 1 deletion lib/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,12 @@ func dealCommon(s string) *CommonConfig {
func dealHost(s string) *file.Host {
h := &file.Host{}
var headerChange string
for _, v := range strings.Split(s, "\n") {
var configDataArr []string
configDataArr = strings.Split(s, "\n")
if len(configDataArr) < 3 {
configDataArr = strings.Split(s, "\r\n")
}
for _, v := range configDataArr {
item := strings.Split(v, "=")
if len(item) == 0 {
continue
Expand Down
6 changes: 2 additions & 4 deletions lib/conn/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ import (
"time"
)

const cryptKey = "1234567812345678"

type Conn struct {
Conn net.Conn
sync.Mutex
Expand Down Expand Up @@ -246,7 +244,7 @@ func (s *Conn) GetHostInfo() (h *file.Host, err error) {
} else {
arr := strings.Split(string(buf[:l]), common.CONN_DATA_SEQ)
h = new(file.Host)
h.Id = file.GetCsvDb().GetHostId()
h.Id = int(file.GetCsvDb().GetHostId())
h.Host = arr[0]
h.Target = arr[1]
h.HeaderChange = arr[2]
Expand Down Expand Up @@ -339,7 +337,7 @@ func (s *Conn) GetTaskInfo() (t *file.Tunnel, err error) {
t.Mode = arr[0]
t.Ports = arr[1]
t.Target = arr[2]
t.Id = file.GetCsvDb().GetTaskId()
t.Id = int(file.GetCsvDb().GetTaskId())
t.Status = true
t.Flow = new(file.Flow)
t.Remark = arr[3]
Expand Down
50 changes: 50 additions & 0 deletions lib/conn/listener.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package conn

import (
"github.com/cnlh/nps/vender/github.com/astaxie/beego/logs"
"github.com/cnlh/nps/vender/github.com/xtaci/kcp"
"net"
"strings"
)

func NewTcpListenerAndProcess(addr string, f func(c net.Conn), listener *net.Listener) error {
var err error
*listener, err = net.Listen("tcp", addr)
if err != nil {
return err
}
Accept(*listener, f)
return nil
}

func NewKcpListenerAndProcess(addr string, f func(c net.Conn)) error {
kcpListener, err := kcp.ListenWithOptions(addr, nil, 150, 3)
if err != nil {
logs.Error(err)
return err
}
for {
c, err := kcpListener.AcceptKCP()
SetUdpSession(c)
if err != nil {
logs.Warn(err)
continue
}
go f(c)
}
return nil
}

func Accept(l net.Listener, f func(c net.Conn)) {
for {
c, err := l.Accept()
if err != nil {
if strings.Contains(err.Error(), "use of closed network connection") {
break
}
logs.Warn(err)
continue
}
go f(c)
}
}
11 changes: 8 additions & 3 deletions lib/crypt/tls.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@ package crypt

import (
"crypto/tls"
"github.com/cnlh/nps/vender/github.com/astaxie/beego"
"github.com/cnlh/nps/vender/github.com/astaxie/beego/logs"
"net"
"os"
"path/filepath"
)

var pemPath, keyPath string

func InitTls(pem, key string) {
pemPath = pem
keyPath = key
}

func NewTlsServerConn(conn net.Conn) net.Conn {
cert, err := tls.LoadX509KeyPair(filepath.Join(beego.AppPath, "conf", "server.pem"), filepath.Join(beego.AppPath, "conf", "server.key"))
cert, err := tls.LoadX509KeyPair(pemPath, keyPath)
if err != nil {
logs.Error(err)
os.Exit(0)
Expand Down
Loading

0 comments on commit b189fb1

Please sign in to comment.