2019-02-23 07:29:48 -08:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2019-08-09 20:15:25 -07:00
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/astaxie/beego/logs"
|
2019-02-23 07:29:48 -08:00
|
|
|
"github.com/cnlh/nps/lib/common"
|
|
|
|
"github.com/cnlh/nps/lib/config"
|
2019-02-26 06:40:28 -08:00
|
|
|
"github.com/cnlh/nps/lib/conn"
|
2019-02-23 07:29:48 -08:00
|
|
|
"github.com/cnlh/nps/lib/crypt"
|
2019-03-02 01:43:21 -08:00
|
|
|
"github.com/cnlh/nps/lib/file"
|
2019-03-01 01:23:14 -08:00
|
|
|
"github.com/cnlh/nps/lib/mux"
|
2019-04-21 08:03:58 -07:00
|
|
|
"github.com/cnlh/nps/server/proxy"
|
2019-08-09 20:10:01 -07:00
|
|
|
"github.com/xtaci/kcp-go"
|
2019-02-23 07:29:48 -08:00
|
|
|
)
|
|
|
|
|
2019-04-08 02:01:08 -07:00
|
|
|
var (
|
2019-04-25 05:13:07 -07:00
|
|
|
LocalServer []*net.TCPListener
|
|
|
|
udpConn net.Conn
|
|
|
|
muxSession *mux.Mux
|
|
|
|
fileServer []*http.Server
|
|
|
|
p2pNetBridge *p2pBridge
|
|
|
|
lock sync.RWMutex
|
|
|
|
udpConnStatus bool
|
2019-04-08 02:01:08 -07:00
|
|
|
)
|
2019-02-23 07:29:48 -08:00
|
|
|
|
2019-04-21 08:03:58 -07:00
|
|
|
type p2pBridge struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p2pBridge *p2pBridge) SendLinkInfo(clientId int, link *conn.Link, t *file.Tunnel) (target net.Conn, err error) {
|
|
|
|
nowConn, err := muxSession.NewConn()
|
|
|
|
if err != nil {
|
|
|
|
udpConn = nil
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if _, err := conn.NewConn(nowConn).SendInfo(link, ""); err != nil {
|
2019-04-25 05:13:07 -07:00
|
|
|
udpConnStatus = false
|
2019-04-21 08:03:58 -07:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return nowConn, nil
|
|
|
|
}
|
|
|
|
|
2019-02-23 07:29:48 -08:00
|
|
|
func CloseLocalServer() {
|
|
|
|
for _, v := range LocalServer {
|
|
|
|
v.Close()
|
|
|
|
}
|
2019-03-02 01:43:21 -08:00
|
|
|
for _, v := range fileServer {
|
|
|
|
v.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func startLocalFileServer(config *config.CommonConfig, t *file.Tunnel, vkey string) {
|
|
|
|
remoteConn, err := NewConn(config.Tp, vkey, config.Server, common.WORK_FILE, config.ProxyUrl)
|
|
|
|
if err != nil {
|
|
|
|
logs.Error("Local connection server failed ", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
srv := &http.Server{
|
|
|
|
Handler: http.StripPrefix(t.StripPre, http.FileServer(http.Dir(t.LocalPath))),
|
|
|
|
}
|
|
|
|
logs.Info("start local file system, local path %s, strip prefix %s ,remote port %s ", t.LocalPath, t.StripPre, t.Ports)
|
|
|
|
fileServer = append(fileServer, srv)
|
2019-03-19 07:41:40 -07:00
|
|
|
listener := mux.NewMux(remoteConn.Conn, common.CONN_TCP)
|
2019-04-08 02:01:08 -07:00
|
|
|
logs.Error(srv.Serve(listener))
|
2019-02-23 07:29:48 -08:00
|
|
|
}
|
|
|
|
|
2019-04-08 03:26:17 -07:00
|
|
|
func StartLocalServer(l *config.LocalServer, config *config.CommonConfig) error {
|
2019-04-25 05:13:07 -07:00
|
|
|
if l.Type != "secret" {
|
|
|
|
go handleUdpMonitor(config, l)
|
2019-04-21 08:03:58 -07:00
|
|
|
}
|
|
|
|
task := &file.Tunnel{
|
|
|
|
Port: l.Port,
|
|
|
|
ServerIp: "0.0.0.0",
|
|
|
|
Status: true,
|
|
|
|
Client: &file.Client{
|
|
|
|
Cnf: &file.Config{
|
|
|
|
U: "",
|
|
|
|
P: "",
|
|
|
|
Compress: config.Client.Cnf.Compress,
|
|
|
|
},
|
|
|
|
Status: true,
|
|
|
|
RateLimit: 0,
|
|
|
|
Flow: &file.Flow{},
|
|
|
|
},
|
|
|
|
Flow: &file.Flow{},
|
|
|
|
Target: &file.Target{},
|
|
|
|
}
|
|
|
|
switch l.Type {
|
|
|
|
case "p2ps":
|
|
|
|
logs.Info("successful start-up of local socks5 monitoring, port", l.Port)
|
|
|
|
return proxy.NewSock5ModeServer(p2pNetBridge, task).Start()
|
|
|
|
case "p2pt":
|
|
|
|
logs.Info("successful start-up of local tcp trans monitoring, port", l.Port)
|
|
|
|
return proxy.NewTunnelModeServer(proxy.HandleTrans, p2pNetBridge, task).Start()
|
|
|
|
case "p2p", "secret":
|
|
|
|
listener, err := net.ListenTCP("tcp", &net.TCPAddr{net.ParseIP("0.0.0.0"), l.Port, ""})
|
|
|
|
if err != nil {
|
|
|
|
logs.Error("local listener startup failed port %d, error %s", l.Port, err.Error())
|
|
|
|
return err
|
2019-03-01 01:23:14 -08:00
|
|
|
}
|
2019-04-21 08:03:58 -07:00
|
|
|
LocalServer = append(LocalServer, listener)
|
|
|
|
logs.Info("successful start-up of local tcp monitoring, port", l.Port)
|
|
|
|
conn.Accept(listener, func(c net.Conn) {
|
|
|
|
logs.Trace("new %s connection", l.Type)
|
|
|
|
if l.Type == "secret" {
|
|
|
|
handleSecret(c, config, l)
|
|
|
|
} else if l.Type == "p2p" {
|
|
|
|
handleP2PVisitor(c, config, l)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2019-02-23 07:29:48 -08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-04-25 05:13:07 -07:00
|
|
|
func handleUdpMonitor(config *config.CommonConfig, l *config.LocalServer) {
|
|
|
|
ticker := time.NewTicker(time.Second * 1)
|
2019-12-01 07:29:36 -08:00
|
|
|
defer ticker.Stop()
|
2019-08-09 20:15:25 -07:00
|
|
|
for {
|
2019-04-25 05:13:07 -07:00
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
|
|
|
if !udpConnStatus {
|
|
|
|
udpConn = nil
|
|
|
|
tmpConn, err := common.GetLocalUdpAddr()
|
|
|
|
if err != nil {
|
|
|
|
logs.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
logs.Notice("try to connect to the server", i+1)
|
|
|
|
newUdpConn(tmpConn.LocalAddr().String(), config, l)
|
|
|
|
if udpConn != nil {
|
|
|
|
udpConnStatus = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-08 02:01:08 -07:00
|
|
|
func handleSecret(localTcpConn net.Conn, config *config.CommonConfig, l *config.LocalServer) {
|
2019-03-01 01:23:14 -08:00
|
|
|
remoteConn, err := NewConn(config.Tp, config.VKey, config.Server, common.WORK_SECRET, config.ProxyUrl)
|
|
|
|
if err != nil {
|
|
|
|
logs.Error("Local connection server failed ", err.Error())
|
2019-03-02 01:43:21 -08:00
|
|
|
return
|
2019-02-26 06:40:28 -08:00
|
|
|
}
|
2019-03-01 01:23:14 -08:00
|
|
|
if _, err := remoteConn.Write([]byte(crypt.Md5(l.Password))); err != nil {
|
|
|
|
logs.Error("Local connection server failed ", err.Error())
|
2019-03-02 01:43:21 -08:00
|
|
|
return
|
2019-03-01 01:23:14 -08:00
|
|
|
}
|
2019-03-25 05:40:22 -07:00
|
|
|
conn.CopyWaitGroup(remoteConn.Conn, localTcpConn, false, false, nil, nil, false, nil)
|
2019-03-01 01:23:14 -08:00
|
|
|
}
|
|
|
|
|
2019-04-08 02:01:08 -07:00
|
|
|
func handleP2PVisitor(localTcpConn net.Conn, config *config.CommonConfig, l *config.LocalServer) {
|
2019-03-01 01:23:14 -08:00
|
|
|
if udpConn == nil {
|
2019-04-21 08:03:58 -07:00
|
|
|
logs.Notice("new conn, P2P can not penetrate successfully, traffic will be transferred through the server")
|
|
|
|
handleSecret(localTcpConn, config, l)
|
2019-03-01 01:23:14 -08:00
|
|
|
}
|
2019-04-08 02:01:08 -07:00
|
|
|
logs.Trace("start trying to connect with the server")
|
2019-03-30 01:35:43 -07:00
|
|
|
//TODO just support compress now because there is not tls file in client packages
|
2019-04-08 02:01:08 -07:00
|
|
|
link := conn.NewLink(common.CONN_TCP, l.Target, false, config.Client.Cnf.Compress, localTcpConn.LocalAddr().String(), false)
|
2019-04-21 08:03:58 -07:00
|
|
|
if target, err := p2pNetBridge.SendLinkInfo(0, link, nil); err != nil {
|
2019-03-01 01:23:14 -08:00
|
|
|
logs.Error(err)
|
2019-04-25 05:13:07 -07:00
|
|
|
udpConnStatus = false
|
2019-03-01 01:23:14 -08:00
|
|
|
return
|
2019-04-21 08:03:58 -07:00
|
|
|
} else {
|
|
|
|
conn.CopyWaitGroup(target, localTcpConn, false, config.Client.Cnf.Compress, nil, nil, false, nil)
|
2019-03-01 01:23:14 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-21 08:03:58 -07:00
|
|
|
func newUdpConn(localAddr string, config *config.CommonConfig, l *config.LocalServer) {
|
2019-04-25 05:13:07 -07:00
|
|
|
lock.Lock()
|
|
|
|
defer lock.Unlock()
|
2019-03-01 01:23:14 -08:00
|
|
|
remoteConn, err := NewConn(config.Tp, config.VKey, config.Server, common.WORK_P2P, config.ProxyUrl)
|
2019-02-23 07:29:48 -08:00
|
|
|
if err != nil {
|
|
|
|
logs.Error("Local connection server failed ", err.Error())
|
2019-03-01 01:23:14 -08:00
|
|
|
return
|
2019-02-23 07:29:48 -08:00
|
|
|
}
|
2019-02-26 06:40:28 -08:00
|
|
|
if _, err := remoteConn.Write([]byte(crypt.Md5(l.Password))); err != nil {
|
2019-02-23 07:29:48 -08:00
|
|
|
logs.Error("Local connection server failed ", err.Error())
|
2019-03-01 01:23:14 -08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
var rAddr []byte
|
|
|
|
//读取服务端地址、密钥 继续做处理
|
|
|
|
if rAddr, err = remoteConn.GetShortLenContent(); err != nil {
|
|
|
|
logs.Error(err)
|
|
|
|
return
|
2019-02-23 07:29:48 -08:00
|
|
|
}
|
2019-04-08 02:01:08 -07:00
|
|
|
var localConn net.PacketConn
|
|
|
|
var remoteAddress string
|
2019-04-21 08:03:58 -07:00
|
|
|
if remoteAddress, localConn, err = handleP2PUdp(localAddr, string(rAddr), crypt.Md5(l.Password), common.WORK_P2P_VISITOR); err != nil {
|
2019-03-01 01:23:14 -08:00
|
|
|
logs.Error(err)
|
2019-03-02 01:43:21 -08:00
|
|
|
return
|
2019-03-01 01:23:14 -08:00
|
|
|
}
|
2019-04-08 02:01:08 -07:00
|
|
|
udpTunnel, err := kcp.NewConn(remoteAddress, nil, 150, 3, localConn)
|
|
|
|
if err != nil || udpTunnel == nil {
|
|
|
|
logs.Warn(err)
|
2019-03-01 01:23:14 -08:00
|
|
|
return
|
2019-02-26 06:40:28 -08:00
|
|
|
}
|
2019-04-08 02:01:08 -07:00
|
|
|
logs.Trace("successful create a connection with server", remoteAddress)
|
|
|
|
conn.SetUdpSession(udpTunnel)
|
|
|
|
udpConn = udpTunnel
|
2019-04-21 08:03:58 -07:00
|
|
|
muxSession = mux.NewMux(udpConn, "kcp")
|
|
|
|
p2pNetBridge = &p2pBridge{}
|
2019-02-23 07:29:48 -08:00
|
|
|
}
|