2019-02-23 07:29:48 -08:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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-02-23 07:29:48 -08:00
|
|
|
"github.com/cnlh/nps/vender/github.com/astaxie/beego/logs"
|
2019-02-26 06:40:28 -08:00
|
|
|
"github.com/cnlh/nps/vender/github.com/xtaci/kcp"
|
2019-02-23 07:29:48 -08:00
|
|
|
"net"
|
2019-03-02 01:43:21 -08:00
|
|
|
"net/http"
|
2019-02-23 07:29:48 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
var LocalServer []*net.TCPListener
|
2019-03-01 01:23:14 -08:00
|
|
|
var udpConn net.Conn
|
|
|
|
var muxSession *mux.Mux
|
2019-03-02 01:43:21 -08:00
|
|
|
var fileServer []*http.Server
|
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-03-02 01:43:21 -08:00
|
|
|
logs.Warn(srv.Serve(listener))
|
2019-02-23 07:29:48 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func StartLocalServer(l *config.LocalServer, config *config.CommonConfig) error {
|
|
|
|
listener, err := net.ListenTCP("tcp", &net.TCPAddr{net.ParseIP("0.0.0.0"), l.Port, ""})
|
|
|
|
if err != nil {
|
2019-03-02 01:43:21 -08:00
|
|
|
logs.Error("local listener startup failed port %d, error %s", l.Port, err.Error())
|
2019-02-23 07:29:48 -08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
LocalServer = append(LocalServer, listener)
|
2019-03-02 01:43:21 -08:00
|
|
|
logs.Info("successful start-up of local monitoring, port", l.Port)
|
2019-03-23 07:19:59 -07:00
|
|
|
conn.Accept(listener, func(c net.Conn) {
|
2019-03-01 01:23:14 -08:00
|
|
|
if l.Type == "secret" {
|
2019-03-23 07:19:59 -07:00
|
|
|
processSecret(c, config, l)
|
2019-03-01 01:23:14 -08:00
|
|
|
} else {
|
2019-03-23 07:19:59 -07:00
|
|
|
processP2P(c, config, l)
|
2019-03-01 01:23:14 -08:00
|
|
|
}
|
2019-03-23 07:19:59 -07:00
|
|
|
})
|
2019-02-23 07:29:48 -08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-03-01 01:23:14 -08:00
|
|
|
func processSecret(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 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
|
|
|
}
|
|
|
|
|
|
|
|
func processP2P(localTcpConn net.Conn, config *config.CommonConfig, l *config.LocalServer) {
|
|
|
|
if udpConn == nil {
|
|
|
|
newUdpConn(config, l)
|
2019-03-02 01:43:21 -08:00
|
|
|
if udpConn == nil {
|
|
|
|
return
|
|
|
|
}
|
2019-03-19 07:41:40 -07:00
|
|
|
muxSession = mux.NewMux(udpConn, "kcp")
|
2019-03-01 01:23:14 -08:00
|
|
|
}
|
|
|
|
nowConn, err := muxSession.NewConn()
|
|
|
|
if err != nil {
|
|
|
|
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 {
|
|
|
|
logs.Error(err)
|
|
|
|
return
|
|
|
|
}
|
2019-03-25 05:40:22 -07:00
|
|
|
conn.CopyWaitGroup(nowConn, localTcpConn, config.Cnf.Crypt, config.Cnf.Compress, nil, nil, false, nil)
|
2019-03-01 01:23:14 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
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 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-03-01 01:23:14 -08:00
|
|
|
//与服务端udp建立连接
|
|
|
|
tmpConn, err := common.GetLocalUdpAddr()
|
|
|
|
if err != nil {
|
|
|
|
logs.Warn(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
//与服务端建立udp连接
|
|
|
|
localAddr, _ := net.ResolveUDPAddr("udp", tmpConn.LocalAddr().String())
|
|
|
|
localConn, err := net.ListenUDP("udp", localAddr)
|
|
|
|
if err != nil {
|
|
|
|
logs.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
localKcpConn, err := kcp.NewConn(string(rAddr), nil, 150, 3, localConn)
|
|
|
|
if err != nil {
|
|
|
|
logs.Error(err)
|
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.SetUdpSession(localKcpConn)
|
2019-03-01 01:23:14 -08:00
|
|
|
//写入密钥、provider身份
|
|
|
|
if _, err := localKcpConn.Write([]byte(crypt.Md5(l.Password))); err != nil {
|
|
|
|
logs.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if _, err := localKcpConn.Write([]byte(common.WORK_P2P_VISITOR)); err != nil {
|
|
|
|
logs.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
//接收服务端传的visitor地址
|
|
|
|
if b, err := conn.NewConn(localKcpConn).GetShortLenContent(); err != nil {
|
|
|
|
logs.Error(err)
|
|
|
|
return
|
2019-02-26 06:40:28 -08:00
|
|
|
} else {
|
2019-03-01 01:23:14 -08:00
|
|
|
//关闭与服务端连接
|
|
|
|
localConn.Close()
|
|
|
|
//建立新的连接
|
|
|
|
localConn, err = net.ListenUDP("udp", localAddr)
|
|
|
|
udpTunnel, err := kcp.NewConn(string(b), nil, 150, 3, localConn)
|
|
|
|
if err != nil || udpTunnel == nil {
|
|
|
|
logs.Warn(err)
|
2019-02-26 06:40:28 -08:00
|
|
|
return
|
|
|
|
}
|
2019-03-01 01:23:14 -08:00
|
|
|
conn.SetUdpSession(udpTunnel)
|
|
|
|
udpConn = udpTunnel
|
2019-02-26 06:40:28 -08:00
|
|
|
}
|
2019-02-23 07:29:48 -08:00
|
|
|
}
|