nps/client/local.go

140 lines
4.2 KiB
Go
Raw Normal View History

2019-02-23 15:29:48 +00:00
package client
import (
"github.com/cnlh/nps/lib/common"
"github.com/cnlh/nps/lib/config"
2019-02-26 14:40:28 +00:00
"github.com/cnlh/nps/lib/conn"
2019-02-23 15:29:48 +00:00
"github.com/cnlh/nps/lib/crypt"
2019-03-02 09:43:21 +00:00
"github.com/cnlh/nps/lib/file"
"github.com/cnlh/nps/lib/mux"
2019-02-23 15:29:48 +00:00
"github.com/cnlh/nps/vender/github.com/astaxie/beego/logs"
2019-02-26 14:40:28 +00:00
"github.com/cnlh/nps/vender/github.com/xtaci/kcp"
2019-02-23 15:29:48 +00:00
"net"
2019-03-02 09:43:21 +00:00
"net/http"
2019-04-08 09:01:08 +00:00
"sync"
2019-02-23 15:29:48 +00:00
)
2019-04-08 09:01:08 +00:00
var (
LocalServer []*net.TCPListener
udpConn net.Conn
muxSession *mux.Mux
fileServer []*http.Server
lock sync.Mutex
)
2019-02-23 15:29:48 +00:00
func CloseLocalServer() {
for _, v := range LocalServer {
v.Close()
}
2019-03-02 09:43:21 +00: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 14:41:40 +00:00
listener := mux.NewMux(remoteConn.Conn, common.CONN_TCP)
2019-04-08 09:01:08 +00:00
logs.Error(srv.Serve(listener))
2019-02-23 15:29:48 +00:00
}
2019-04-08 10:26:17 +00:00
func StartLocalServer(l *config.LocalServer, config *config.CommonConfig) error {
2019-02-23 15:29:48 +00:00
listener, err := net.ListenTCP("tcp", &net.TCPAddr{net.ParseIP("0.0.0.0"), l.Port, ""})
if err != nil {
2019-03-02 09:43:21 +00:00
logs.Error("local listener startup failed port %d, error %s", l.Port, err.Error())
2019-02-23 15:29:48 +00:00
return err
}
LocalServer = append(LocalServer, listener)
2019-03-02 09:43:21 +00:00
logs.Info("successful start-up of local monitoring, port", l.Port)
2019-03-23 14:19:59 +00:00
conn.Accept(listener, func(c net.Conn) {
if l.Type == "secret" {
2019-04-08 09:01:08 +00:00
handleSecret(c, config, l)
} else {
2019-04-08 09:01:08 +00:00
handleP2PVisitor(c, config, l)
}
2019-03-23 14:19:59 +00:00
})
2019-02-23 15:29:48 +00:00
return nil
}
2019-04-08 09:01:08 +00:00
func handleSecret(localTcpConn net.Conn, config *config.CommonConfig, l *config.LocalServer) {
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 09:43:21 +00:00
return
2019-02-26 14:40:28 +00:00
}
if _, err := remoteConn.Write([]byte(crypt.Md5(l.Password))); err != nil {
logs.Error("Local connection server failed ", err.Error())
2019-03-02 09:43:21 +00:00
return
}
2019-03-25 12:40:22 +00:00
conn.CopyWaitGroup(remoteConn.Conn, localTcpConn, false, false, nil, nil, false, nil)
}
2019-04-08 09:01:08 +00:00
func handleP2PVisitor(localTcpConn net.Conn, config *config.CommonConfig, l *config.LocalServer) {
restart:
lock.Lock()
if udpConn == nil {
newUdpConn(config, l)
2019-03-02 09:43:21 +00:00
if udpConn == nil {
2019-04-08 09:01:08 +00:00
lock.Unlock()
2019-03-02 09:43:21 +00:00
return
}
2019-03-19 14:41:40 +00:00
muxSession = mux.NewMux(udpConn, "kcp")
}
2019-04-08 09:01:08 +00:00
lock.Unlock()
logs.Trace("start trying to connect with the server")
nowConn, err := muxSession.NewConn()
if err != nil {
2019-04-08 09:01:08 +00:00
udpConn = nil
logs.Error(err, "reconnect......")
goto restart
return
}
2019-03-30 08:35:43 +00:00
//TODO just support compress now because there is not tls file in client packages
2019-04-08 09:01:08 +00:00
link := conn.NewLink(common.CONN_TCP, l.Target, false, config.Client.Cnf.Compress, localTcpConn.LocalAddr().String(), false)
2019-03-30 08:35:43 +00:00
if _, err := conn.NewConn(nowConn).SendInfo(link, ""); err != nil {
logs.Error(err)
return
}
2019-03-30 08:35:43 +00:00
conn.CopyWaitGroup(nowConn, localTcpConn, false, config.Client.Cnf.Compress, nil, nil, false, nil)
}
func newUdpConn(config *config.CommonConfig, l *config.LocalServer) {
remoteConn, err := NewConn(config.Tp, config.VKey, config.Server, common.WORK_P2P, config.ProxyUrl)
2019-02-23 15:29:48 +00:00
if err != nil {
logs.Error("Local connection server failed ", err.Error())
return
2019-02-23 15:29:48 +00:00
}
2019-02-26 14:40:28 +00:00
if _, err := remoteConn.Write([]byte(crypt.Md5(l.Password))); err != nil {
2019-02-23 15:29:48 +00:00
logs.Error("Local connection server failed ", err.Error())
return
}
var rAddr []byte
//读取服务端地址、密钥 继续做处理
if rAddr, err = remoteConn.GetShortLenContent(); err != nil {
logs.Error(err)
return
2019-02-23 15:29:48 +00:00
}
2019-04-08 09:01:08 +00:00
var localConn net.PacketConn
var remoteAddress string
if remoteAddress, localConn, err = handleP2PUdp(string(rAddr), crypt.Md5(l.Password), common.WORK_P2P_VISITOR); err != nil {
logs.Error(err)
2019-03-02 09:43:21 +00:00
return
}
2019-04-08 09:01:08 +00:00
udpTunnel, err := kcp.NewConn(remoteAddress, nil, 150, 3, localConn)
if err != nil || udpTunnel == nil {
logs.Warn(err)
return
2019-02-26 14:40:28 +00:00
}
2019-04-08 09:01:08 +00:00
logs.Trace("successful create a connection with server", remoteAddress)
conn.SetUdpSession(udpTunnel)
udpConn = udpTunnel
2019-02-23 15:29:48 +00:00
}