Skip to content

Commit

Permalink
Ip限制 npc代理连接
Browse files Browse the repository at this point in the history
  • Loading branch information
刘河 committed Feb 16, 2019
1 parent 9f6b33a commit 3b18d66
Show file tree
Hide file tree
Showing 64 changed files with 1,414 additions and 132 deletions.
63 changes: 45 additions & 18 deletions bridge/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package bridge
import (
"encoding/binary"
"errors"
"fmt"
"github.com/cnlh/nps/lib/common"
"github.com/cnlh/nps/lib/conn"
"github.com/cnlh/nps/lib/crypt"
"github.com/cnlh/nps/lib/file"
"github.com/cnlh/nps/lib/kcp"
"github.com/cnlh/nps/lib/lg"
"github.com/cnlh/nps/lib/pool"
"github.com/cnlh/nps/server/tool"
"github.com/cnlh/nps/vender/github.com/xtaci/kcp"
"log"
"net"
"strconv"
Expand Down Expand Up @@ -38,25 +39,28 @@ func NewClient(t *conn.Conn, s *conn.Conn) *Client {
}

type Bridge struct {
TunnelPort int //通信隧道端口
tcpListener *net.TCPListener //server端监听
kcpListener *kcp.Listener //server端监听
Client map[int]*Client
tunnelType string //bridge type kcp or tcp
OpenTask chan *file.Tunnel
CloseClient chan int
lock sync.Mutex
tunnelLock sync.Mutex
clientLock sync.RWMutex
TunnelPort int //通信隧道端口
tcpListener *net.TCPListener //server端监听
kcpListener *kcp.Listener //server端监听
Client map[int]*Client
tunnelType string //bridge type kcp or tcp
OpenTask chan *file.Tunnel
CloseClient chan int
clientLock sync.RWMutex
Register map[string]time.Time
registerLock sync.RWMutex
ipVerify bool
}

func NewTunnel(tunnelPort int, tunnelType string) *Bridge {
func NewTunnel(tunnelPort int, tunnelType string, ipVerify bool) *Bridge {
t := new(Bridge)
t.TunnelPort = tunnelPort
t.Client = make(map[int]*Client)
t.tunnelType = tunnelType
t.OpenTask = make(chan *file.Tunnel)
t.CloseClient = make(chan int)
t.Register = make(map[string]time.Time)
t.ipVerify = ipVerify
return t
}

Expand Down Expand Up @@ -128,7 +132,6 @@ func (s *Bridge) cliProcess(c *conn.Conn) {
if flag, err := c.ReadFlag(); err == nil {
s.typeDeal(flag, c, id)
} else {
log.Println(222)
log.Println(err, flag)
}
return
Expand Down Expand Up @@ -180,13 +183,25 @@ func (s *Bridge) typeDeal(typeVal string, c *conn.Conn, id int) {
go s.clientCopy(id)
case common.WORK_CONFIG:
go s.GetConfig(c)
case common.WORK_REGISTER:
go s.register(c)
}
c.SetAlive(s.tunnelType)
return
}

func (s *Bridge) register(c *conn.Conn) {
var hour int32
if err := binary.Read(c, binary.LittleEndian, &hour); err == nil {
s.registerLock.Lock()
s.Register[common.GetIpByAddr(c.Conn.RemoteAddr().String())] = time.Now().Add(time.Hour * time.Duration(hour))
lg.Println(s.Register[common.GetIpByAddr(c.Conn.RemoteAddr().String())])
s.registerLock.Unlock()
}
}

//等待
func (s *Bridge) waitStatus(clientId, id int) (bool) {
func (s *Bridge) waitStatus(clientId, id int) bool {
ticker := time.NewTicker(time.Millisecond * 100)
stop := time.After(time.Second * 10)
for {
Expand All @@ -209,13 +224,26 @@ func (s *Bridge) waitStatus(clientId, id int) (bool) {
return false
}
}
return false
}

func (s *Bridge) SendLinkInfo(clientId int, link *conn.Link) (tunnel *conn.Conn, err error) {
func (s *Bridge) SendLinkInfo(clientId int, link *conn.Link, linkAddr string) (tunnel *conn.Conn, err error) {
s.clientLock.Lock()
if v, ok := s.Client[clientId]; ok {
s.clientLock.Unlock()
if s.ipVerify {
s.registerLock.Lock()
ip := common.GetIpByAddr(linkAddr)
if v, ok := s.Register[ip]; !ok {
s.registerLock.Unlock()
return nil, errors.New(fmt.Sprintf("The ip %s is not in the validation list", ip))
} else {
if !v.After(time.Now()) {
return nil, errors.New(fmt.Sprintf("The validity of the ip %s has expired", ip))
}
}
s.registerLock.Unlock()
}

v.signal.SendLinkInfo(link)
if err != nil {
lg.Println("send link information error:", err, link.Id)
Expand Down Expand Up @@ -300,7 +328,7 @@ func (s *Bridge) GetConfig(c *conn.Conn) {
fail = true
c.WriteAddFail()
break
} else if file.GetCsvDb().IsHostExist(h.Host) {
} else if file.GetCsvDb().IsHostExist(h) {
fail = true
c.WriteAddFail()
} else {
Expand Down Expand Up @@ -383,7 +411,6 @@ func (s *Bridge) clientCopy(clientId int) {
for {
if id, err := client.tunnel.GetLen(); err != nil {
s.closeClient(clientId)
lg.Println("读取msg id 错误", err, id)
break
} else {
client.Lock()
Expand Down
34 changes: 25 additions & 9 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import (
"github.com/cnlh/nps/lib/common"
"github.com/cnlh/nps/lib/config"
"github.com/cnlh/nps/lib/conn"
"github.com/cnlh/nps/lib/kcp"
"github.com/cnlh/nps/lib/lg"
"github.com/cnlh/nps/lib/pool"
"github.com/cnlh/nps/vender/github.com/xtaci/kcp"
"github.com/cnlh/nps/vender/golang.org/x/net/proxy"
"io/ioutil"
"net"
"net/url"
"path/filepath"
"sync"
"time"
Expand All @@ -21,26 +23,28 @@ type TRPClient struct {
tunnel *conn.Conn
bridgeConnType string
stop chan bool
proxyUrl string
sync.Mutex
vKey string
}

//new client
func NewRPClient(svraddr string, vKey string, bridgeConnType string) *TRPClient {
func NewRPClient(svraddr string, vKey string, bridgeConnType string, proxyUrl string) *TRPClient {
return &TRPClient{
svrAddr: svraddr,
linkMap: make(map[int]*conn.Link),
Mutex: sync.Mutex{},
vKey: vKey,
bridgeConnType: bridgeConnType,
stop: make(chan bool),
proxyUrl: proxyUrl,
}
}

//start
func (s *TRPClient) Start() {
retry:
c, err := NewConn(s.bridgeConnType, s.vKey, s.svrAddr, common.WORK_MAIN)
c, err := NewConn(s.bridgeConnType, s.vKey, s.svrAddr, common.WORK_MAIN, s.proxyUrl)
if err != nil {
lg.Println("The connection server failed and will be reconnected in five seconds")
time.Sleep(time.Second * 5)
Expand Down Expand Up @@ -130,7 +134,7 @@ func (s *TRPClient) linkProcess(link *conn.Link, c *conn.Conn) {
//隧道模式处理
func (s *TRPClient) dealChan() {
var err error
s.tunnel, err = NewConn(s.bridgeConnType, s.vKey, s.svrAddr, common.WORK_CHAN)
s.tunnel, err = NewConn(s.bridgeConnType, s.vKey, s.svrAddr, common.WORK_CHAN, s.proxyUrl)
if err != nil {
lg.Println("connect to ", s.svrAddr, "error:", err)
return
Expand Down Expand Up @@ -184,7 +188,7 @@ re:
return
}
first = false
c, err := NewConn(cnf.CommonConfig.Tp, cnf.CommonConfig.VKey, cnf.CommonConfig.Server, common.WORK_CONFIG)
c, err := NewConn(cnf.CommonConfig.Tp, cnf.CommonConfig.VKey, cnf.CommonConfig.Server, common.WORK_CONFIG, cnf.CommonConfig.ProxyUrl)
if err != nil {
lg.Println(err)
goto re
Expand Down Expand Up @@ -220,24 +224,36 @@ re:
goto re
}
if !c.GetAddStatus() {
lg.Println(errAdd, v.Port)
lg.Println(errAdd, v.Ports)
goto re
}
}

c.Close()

NewRPClient(cnf.CommonConfig.Server, string(b), cnf.CommonConfig.Tp).Start()
NewRPClient(cnf.CommonConfig.Server, string(b), cnf.CommonConfig.Tp, cnf.CommonConfig.ProxyUrl).Start()
goto re
}

//Create a new connection with the server and verify it
func NewConn(tp string, vkey string, server string, connType string) (*conn.Conn, error) {
func NewConn(tp string, vkey string, server string, connType string, proxyUrl string) (*conn.Conn, error) {
var err error
var connection net.Conn
var sess *kcp.UDPSession
if tp == "tcp" {
connection, err = net.Dial("tcp", server)
if proxyUrl != "" {
u, er := url.Parse(proxyUrl)
if er != nil {
return nil, er
}
n, er := proxy.FromURL(u, nil)
if er != nil {
return nil, er
}
connection, err = n.Dial("tcp", server)
} else {
connection, err = net.Dial("tcp", server)
}
} else {
sess, err = kcp.DialWithOptions(server, nil, 10, 3)
conn.SetUdpSession(sess)
Expand Down
8 changes: 4 additions & 4 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ func TestConfig(t *testing.T) {
RWMutex: sync.RWMutex{},
}
tunnel := &file.Tunnel{
Port: 9001,
Mode: "tcpServer",
Target: "127.0.0.1:8082",
Remark: "333",
Port: 9001,
Mode: "tcpServer",
Target: "127.0.0.1:8082",
Remark: "333",
}
var b []byte
if b, err = c.ReadLen(16); err != nil {
Expand Down
20 changes: 20 additions & 0 deletions client/register.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package client

import (
"encoding/binary"
"github.com/cnlh/nps/lib/common"
"log"
"os"
)

func RegisterLocalIp(server string, vKey string, tp string, proxyUrl string, hour int) {
c, err := NewConn(tp, vKey, server, common.WORK_REGISTER, proxyUrl)
if err != nil {
log.Fatalln(err)
}
if err := binary.Write(c, binary.LittleEndian, int32(hour)); err != nil {
log.Fatalln(err)
}
log.Printf("Successful ip registration for local public network, the validity period is %d hours.", hour)
os.Exit(0)
}
52 changes: 52 additions & 0 deletions client/status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package client

import (
"github.com/cnlh/nps/lib/common"
"github.com/cnlh/nps/lib/config"
"github.com/cnlh/nps/lib/lg"
"log"
"os"
"path/filepath"
"strings"
)

func GetTaskStatus(path string) {
cnf, err := config.NewConfig(path)
if err != nil {
log.Fatalln(err)
}
c, err := NewConn(cnf.CommonConfig.Tp, cnf.CommonConfig.VKey, cnf.CommonConfig.Server, common.WORK_CONFIG, cnf.CommonConfig.ProxyUrl)
if err != nil {
log.Fatalln(err)
}
if _, err := c.Write([]byte(common.WORK_STATUS)); err != nil {
log.Fatalln(err)
}
if f, err := common.ReadAllFromFile(filepath.Join(common.GetTmpPath(), "npc_vkey.txt")); err != nil {
log.Fatalln(err)
} else if _, err := c.Write([]byte(string(f))); err != nil {
log.Fatalln(err)
}
if l, err := c.GetLen(); err != nil {
log.Fatalln(err)
} else if b, err := c.ReadLen(l); err != nil {
lg.Fatalln(err)
} else {
arr := strings.Split(string(b), common.CONN_DATA_SEQ)
for _, v := range cnf.Hosts {
if common.InArr(arr, v.Remark) {
log.Println(v.Remark, "ok")
} else {
log.Println(v.Remark, "not running")
}
}
for _, v := range cnf.Tasks {
if common.InArr(arr, v.Remark) {
log.Println(v.Remark, "ok")
} else {
log.Println(v.Remark, "not running")
}
}
}
os.Exit(0)
}
Loading

0 comments on commit 3b18d66

Please sign in to comment.