目录变更

This commit is contained in:
刘河
2019-02-03 17:25:00 +08:00
parent 87f2c8b2ce
commit 74b262503e
27 changed files with 328 additions and 203 deletions

View File

@@ -3,7 +3,7 @@ package server
import (
"errors"
"github.com/cnlh/nps/bridge"
"github.com/cnlh/nps/utils"
"github.com/cnlh/nps/lib"
"net"
"net/http"
"sync"
@@ -13,8 +13,8 @@ import (
type server struct {
id int
bridge *bridge.Bridge
task *utils.Tunnel
config *utils.Config
task *lib.Tunnel
config *lib.Config
errorContent []byte
sync.Mutex
}
@@ -26,7 +26,7 @@ func (s *server) FlowAdd(in, out int64) {
s.task.Flow.InletFlow += in
}
func (s *server) FlowAddHost(host *utils.Host, in, out int64) {
func (s *server) FlowAddHost(host *lib.Host, in, out int64) {
s.Lock()
defer s.Unlock()
host.Flow.ExportFlow += out
@@ -62,11 +62,11 @@ func (s *server) ResetConfig() bool {
}
}
s.task.Client.Rate = client.Rate
s.config.CompressDecode, s.config.CompressEncode = utils.GetCompressType(s.config.Compress)
s.config.CompressDecode, s.config.CompressEncode = lib.GetCompressType(s.config.Compress)
return true
}
func (s *server) linkCopy(link *utils.Link, c *utils.Conn, rb []byte, tunnel *utils.Conn, flow *utils.Flow) {
func (s *server) linkCopy(link *lib.Link, c *lib.Conn, rb []byte, tunnel *lib.Conn, flow *lib.Flow) {
if rb != nil {
if _, err := tunnel.SendMsg(rb, link); err != nil {
c.Close()
@@ -75,31 +75,31 @@ func (s *server) linkCopy(link *utils.Link, c *utils.Conn, rb []byte, tunnel *ut
flow.Add(len(rb), 0)
}
for {
buf := utils.BufPoolCopy.Get().([]byte)
buf := lib.BufPoolCopy.Get().([]byte)
if n, err := c.Read(buf); err != nil {
tunnel.SendMsg([]byte(utils.IO_EOF), link)
tunnel.SendMsg([]byte(lib.IO_EOF), link)
break
} else {
if _, err := tunnel.SendMsg(buf[:n], link); err != nil {
utils.PutBufPoolCopy(buf)
lib.PutBufPoolCopy(buf)
c.Close()
break
}
utils.PutBufPoolCopy(buf)
lib.PutBufPoolCopy(buf)
flow.Add(n, 0)
}
}
}
func (s *server) writeConnFail(c net.Conn) {
c.Write([]byte(utils.ConnectionFailBytes))
c.Write([]byte(lib.ConnectionFailBytes))
c.Write(s.errorContent)
}
//权限认证
func (s *server) auth(r *http.Request, c *utils.Conn, u, p string) error {
if u != "" && p != "" && !utils.CheckAuth(r, u, p) {
c.Write([]byte(utils.UnauthorizedBytes))
func (s *server) auth(r *http.Request, c *lib.Conn, u, p string) error {
if u != "" && p != "" && !lib.CheckAuth(r, u, p) {
c.Write([]byte(lib.UnauthorizedBytes))
c.Close()
return errors.New("401 Unauthorized")
}

View File

@@ -5,7 +5,7 @@ import (
"crypto/tls"
"github.com/astaxie/beego"
"github.com/cnlh/nps/bridge"
"github.com/cnlh/nps/utils"
"github.com/cnlh/nps/lib"
"net"
"net/http"
"net/http/httputil"
@@ -22,7 +22,7 @@ type httpServer struct {
stop chan bool
}
func NewHttp(bridge *bridge.Bridge, c *utils.Tunnel) *httpServer {
func NewHttp(bridge *bridge.Bridge, c *lib.Tunnel) *httpServer {
httpPort, _ := beego.AppConfig.Int("httpProxyPort")
httpsPort, _ := beego.AppConfig.Int("httpsProxyPort")
pemPath := beego.AppConfig.String("pemPath")
@@ -44,39 +44,39 @@ func NewHttp(bridge *bridge.Bridge, c *utils.Tunnel) *httpServer {
func (s *httpServer) Start() error {
var err error
var http, https *http.Server
if s.errorContent, err = utils.ReadAllFromFile(beego.AppPath + "/web/static/page/error.html"); err != nil {
if s.errorContent, err = lib.ReadAllFromFile(beego.AppPath + "/web/static/page/error.html"); err != nil {
s.errorContent = []byte("easyProxy 404")
}
if s.httpPort > 0 {
if !s.TestTcpPort(s.httpPort) {
utils.Fatalln("http端口", s.httpPort, "被占用!")
lib.Fatalln("http端口", s.httpPort, "被占用!")
}
http = s.NewServer(s.httpPort)
go func() {
utils.Println("启动http监听,端口为", s.httpPort)
lib.Println("启动http监听,端口为", s.httpPort)
err := http.ListenAndServe()
if err != nil {
utils.Fatalln(err)
lib.Fatalln(err)
}
}()
}
if s.httpsPort > 0 {
if !s.TestTcpPort(s.httpsPort) {
utils.Fatalln("https端口", s.httpsPort, "被占用!")
lib.Fatalln("https端口", s.httpsPort, "被占用!")
}
if !utils.FileExists(s.pemPath) {
utils.Fatalf("ssl certFile文件%s不存在", s.pemPath)
if !lib.FileExists(s.pemPath) {
lib.Fatalf("ssl certFile文件%s不存在", s.pemPath)
}
if !utils.FileExists(s.keyPath) {
utils.Fatalf("ssl keyFile文件%s不存在", s.keyPath)
if !lib.FileExists(s.keyPath) {
lib.Fatalf("ssl keyFile文件%s不存在", s.keyPath)
}
https = s.NewServer(s.httpsPort)
go func() {
utils.Println("启动https监听,端口为", s.httpsPort)
lib.Println("启动https监听,端口为", s.httpsPort)
err := https.ListenAndServeTLS(s.pemPath, s.keyPath)
if err != nil {
utils.Fatalln(err)
lib.Fatalln(err)
}
}()
}
@@ -108,35 +108,35 @@ func (s *httpServer) handleTunneling(w http.ResponseWriter, r *http.Request) {
if err != nil {
http.Error(w, err.Error(), http.StatusServiceUnavailable)
}
s.process(utils.NewConn(conn), r)
s.process(lib.NewConn(conn), r)
}
func (s *httpServer) process(c *utils.Conn, r *http.Request) {
func (s *httpServer) process(c *lib.Conn, r *http.Request) {
//多客户端域名代理
var (
isConn = true
link *utils.Link
host *utils.Host
tunnel *utils.Conn
link *lib.Link
host *lib.Host
tunnel *lib.Conn
err error
)
for {
//首次获取conn
if isConn {
if host, err = GetInfoByHost(r.Host); err != nil {
utils.Printf("the host %s is not found !", r.Host)
lib.Printf("the host %s is not found !", r.Host)
break
}
//流量限制
if host.Client.Flow.FlowLimit > 0 && (host.Client.Flow.FlowLimit<<20) < (host.Client.Flow.ExportFlow+host.Client.Flow.InletFlow) {
break
}
host.Client.Cnf.CompressDecode, host.Client.Cnf.CompressEncode = utils.GetCompressType(host.Client.Cnf.Compress)
host.Client.Cnf.CompressDecode, host.Client.Cnf.CompressEncode = lib.GetCompressType(host.Client.Cnf.Compress)
//权限控制
if err = s.auth(r, c, host.Client.Cnf.U, host.Client.Cnf.P); err != nil {
break
}
link = utils.NewLink(host.Client.GetId(), utils.CONN_TCP, host.GetRandomTarget(), host.Client.Cnf.CompressEncode, host.Client.Cnf.CompressDecode, host.Client.Cnf.Crypt, c, host.Flow, nil, host.Client.Rate, nil)
link = lib.NewLink(host.Client.GetId(), lib.CONN_TCP, host.GetRandomTarget(), host.Client.Cnf.CompressEncode, host.Client.Cnf.CompressDecode, host.Client.Cnf.Crypt, c, host.Flow, nil, host.Client.Rate, nil)
if tunnel, err = s.bridge.SendLinkInfo(host.Client.Id, link); err != nil {
break
}
@@ -148,7 +148,7 @@ func (s *httpServer) process(c *utils.Conn, r *http.Request) {
}
}
//根据设定修改header和host
utils.ChangeHostAndHeader(r, host.HostChange, host.HeaderChange, c.Conn.RemoteAddr().String())
lib.ChangeHostAndHeader(r, host.HostChange, host.HeaderChange, c.Conn.RemoteAddr().String())
b, err := httputil.DumpRequest(r, true)
if err != nil {
break
@@ -163,7 +163,7 @@ func (s *httpServer) process(c *utils.Conn, r *http.Request) {
if isConn {
s.writeConnFail(c.Conn)
} else {
tunnel.SendMsg([]byte(utils.IO_EOF), link)
tunnel.SendMsg([]byte(lib.IO_EOF), link)
}
c.Close()

View File

@@ -3,7 +3,7 @@ package server
import (
"errors"
"github.com/cnlh/nps/bridge"
"github.com/cnlh/nps/utils"
"github.com/cnlh/nps/lib"
"log"
"os"
"reflect"
@@ -13,7 +13,7 @@ import (
var (
Bridge *bridge.Bridge
RunList map[int]interface{} //运行中的任务
CsvDb = utils.GetCsvDb()
CsvDb = lib.GetCsvDb()
startFinish chan bool
)
@@ -26,27 +26,27 @@ func init() {
func InitFromCsv() {
for _, v := range CsvDb.Tasks {
if v.Status {
utils.Println("启动模式:", v.Mode, "监听端口:", v.TcpPort)
lib.Println("启动模式:", v.Mode, "监听端口:", v.TcpPort)
AddTask(v)
}
}
}
//start a new server
func StartNewServer(bridgePort int, cnf *utils.Tunnel, test bool) {
func StartNewServer(bridgePort int, cnf *lib.Tunnel, test bool) {
go func() {
Bridge = bridge.NewTunnel(bridgePort, RunList)
if err := Bridge.StartTunnel(); err != nil {
utils.Fatalln("服务端开启失败", err)
lib.Fatalln("服务端开启失败", err)
}
if svr := NewMode(Bridge, cnf); svr != nil {
RunList[cnf.Id] = svr
err := reflect.ValueOf(svr).MethodByName("Start").Call(nil)[0]
if err.Interface() != nil {
utils.Fatalln(err)
lib.Fatalln(err)
}
} else {
utils.Fatalln("启动模式不正确")
lib.Fatalln("启动模式不正确")
}
}()
for {
@@ -61,7 +61,7 @@ func StartNewServer(bridgePort int, cnf *utils.Tunnel, test bool) {
}
//new a server by mode name
func NewMode(Bridge *bridge.Bridge, c *utils.Tunnel) interface{} {
func NewMode(Bridge *bridge.Bridge, c *lib.Tunnel) interface{} {
switch c.Mode {
case "tunnelServer":
return NewTunnelModeServer(ProcessTunnel, Bridge, c)
@@ -73,11 +73,11 @@ func NewMode(Bridge *bridge.Bridge, c *utils.Tunnel) interface{} {
return NewUdpModeServer(Bridge, c)
case "webServer":
InitFromCsv()
t := &utils.Tunnel{
t := &lib.Tunnel{
TcpPort: 0,
Mode: "httpHostServer",
Target: "",
Config: &utils.Config{},
Config: &lib.Config{},
Status: true,
}
AddTask(t)
@@ -106,13 +106,13 @@ func StopServer(id int) error {
}
//add task
func AddTask(t *utils.Tunnel) error {
func AddTask(t *lib.Tunnel) error {
if svr := NewMode(Bridge, t); svr != nil {
RunList[t.Id] = svr
go func() {
err := reflect.ValueOf(svr).MethodByName("Start").Call(nil)[0]
if err.Interface() != nil {
utils.Fatalln("服务端", t.Id, "启动失败,错误:", err)
lib.Fatalln("服务端", t.Id, "启动失败,错误:", err)
delete(RunList, t.Id)
}
}()
@@ -143,7 +143,7 @@ func DelTask(id int) error {
}
//get key by host from x
func GetInfoByHost(host string) (h *utils.Host, err error) {
func GetInfoByHost(host string) (h *lib.Host, err error) {
for _, v := range CsvDb.Hosts {
s := strings.Split(host, ":")
if s[0] == v.Host {
@@ -156,8 +156,8 @@ func GetInfoByHost(host string) (h *utils.Host, err error) {
}
//get task list by page num
func GetTunnel(start, length int, typeVal string, clientId int) ([]*utils.Tunnel, int) {
list := make([]*utils.Tunnel, 0)
func GetTunnel(start, length int, typeVal string, clientId int) ([]*lib.Tunnel, int) {
list := make([]*lib.Tunnel, 0)
var cnt int
for _, v := range CsvDb.Tasks {
if (typeVal != "" && v.Mode != typeVal) || (typeVal == "" && clientId != v.Client.Id) {
@@ -184,13 +184,13 @@ func GetTunnel(start, length int, typeVal string, clientId int) ([]*utils.Tunnel
}
//获取客户端列表
func GetClientList(start, length int) (list []*utils.Client, cnt int) {
func GetClientList(start, length int) (list []*lib.Client, cnt int) {
list, cnt = CsvDb.GetClientList(start, length)
dealClientData(list)
return
}
func dealClientData(list []*utils.Client) {
func dealClientData(list []*lib.Client) {
for _, v := range list {
if _, ok := Bridge.Client[v.Id]; ok {
v.IsConnect = true

View File

@@ -4,7 +4,7 @@ import (
"encoding/binary"
"errors"
"github.com/cnlh/nps/bridge"
"github.com/cnlh/nps/utils"
"github.com/cnlh/nps/lib"
"io"
"net"
"strconv"
@@ -65,7 +65,7 @@ func (s *Sock5ModeServer) handleRequest(c net.Conn) {
_, err := io.ReadFull(c, header)
if err != nil {
utils.Println("illegal request", err)
lib.Println("illegal request", err)
c.Close()
return
}
@@ -135,18 +135,18 @@ func (s *Sock5ModeServer) doConnect(c net.Conn, command uint8) {
addr := net.JoinHostPort(host, strconv.Itoa(int(port)))
var ltype string
if command == associateMethod {
ltype = utils.CONN_UDP
ltype = lib.CONN_UDP
} else {
ltype = utils.CONN_TCP
ltype = lib.CONN_TCP
}
link := utils.NewLink(s.task.Client.GetId(), ltype, addr, s.config.CompressEncode, s.config.CompressDecode, s.config.Crypt, utils.NewConn(c), s.task.Flow, nil, s.task.Client.Rate, nil)
link := lib.NewLink(s.task.Client.GetId(), ltype, addr, s.config.CompressEncode, s.config.CompressDecode, s.config.Crypt, lib.NewConn(c), s.task.Flow, nil, s.task.Client.Rate, nil)
if tunnel, err := s.bridge.SendLinkInfo(s.task.Client.Id, link); err != nil {
c.Close()
return
} else {
s.sendReply(c, succeeded)
s.linkCopy(link, utils.NewConn(c), nil, tunnel, s.task.Flow)
s.linkCopy(link, lib.NewConn(c), nil, tunnel, s.task.Flow)
}
return
}
@@ -162,7 +162,7 @@ func (s *Sock5ModeServer) handleBind(c net.Conn) {
//udp
func (s *Sock5ModeServer) handleUDP(c net.Conn) {
utils.Println("UDP Associate")
lib.Println("UDP Associate")
/*
+----+------+------+----------+----------+----------+
|RSV | FRAG | ATYP | DST.ADDR | DST.PORT | DATA |
@@ -175,7 +175,7 @@ func (s *Sock5ModeServer) handleUDP(c net.Conn) {
// relay udp datagram silently, without any notification to the requesting client
if buf[2] != 0 {
// does not support fragmentation, drop it
utils.Println("does not support fragmentation, drop")
lib.Println("does not support fragmentation, drop")
dummy := make([]byte, maxUDPPacketSize)
c.Read(dummy)
}
@@ -187,13 +187,13 @@ func (s *Sock5ModeServer) handleUDP(c net.Conn) {
func (s *Sock5ModeServer) handleConn(c net.Conn) {
buf := make([]byte, 2)
if _, err := io.ReadFull(c, buf); err != nil {
utils.Println("negotiation err", err)
lib.Println("negotiation err", err)
c.Close()
return
}
if version := buf[0]; version != 5 {
utils.Println("only support socks5, request from: ", c.RemoteAddr())
lib.Println("only support socks5, request from: ", c.RemoteAddr())
c.Close()
return
}
@@ -201,7 +201,7 @@ func (s *Sock5ModeServer) handleConn(c net.Conn) {
methods := make([]byte, nMethods)
if len, err := c.Read(methods); len != int(nMethods) || err != nil {
utils.Println("wrong method")
lib.Println("wrong method")
c.Close()
return
}
@@ -210,7 +210,7 @@ func (s *Sock5ModeServer) handleConn(c net.Conn) {
c.Write(buf)
if err := s.Auth(c); err != nil {
c.Close()
utils.Println("验证失败:", err)
lib.Println("验证失败:", err)
return
}
} else {
@@ -269,7 +269,7 @@ func (s *Sock5ModeServer) Start() error {
if strings.Contains(err.Error(), "use of closed network connection") {
break
}
utils.Fatalln("accept error: ", err)
lib.Fatalln("accept error: ", err)
}
if !s.ResetConfig() {
conn.Close()
@@ -286,11 +286,11 @@ func (s *Sock5ModeServer) Close() error {
}
//new
func NewSock5ModeServer(bridge *bridge.Bridge, task *utils.Tunnel) *Sock5ModeServer {
func NewSock5ModeServer(bridge *bridge.Bridge, task *lib.Tunnel) *Sock5ModeServer {
s := new(Sock5ModeServer)
s.bridge = bridge
s.task = task
s.config = utils.DeepCopyConfig(task.Config)
s.config = lib.DeepCopyConfig(task.Config)
if s.config.U != "" && s.config.P != "" {
s.isVerify = true
} else {

View File

@@ -4,7 +4,7 @@ import (
"errors"
"github.com/astaxie/beego"
"github.com/cnlh/nps/bridge"
"github.com/cnlh/nps/utils"
"github.com/cnlh/nps/lib"
"net"
"strings"
)
@@ -16,12 +16,12 @@ type TunnelModeServer struct {
}
//tcp|http|host
func NewTunnelModeServer(process process, bridge *bridge.Bridge, task *utils.Tunnel) *TunnelModeServer {
func NewTunnelModeServer(process process, bridge *bridge.Bridge, task *lib.Tunnel) *TunnelModeServer {
s := new(TunnelModeServer)
s.bridge = bridge
s.process = process
s.task = task
s.config = utils.DeepCopyConfig(task.Config)
s.config = lib.DeepCopyConfig(task.Config)
return s
}
@@ -38,17 +38,17 @@ func (s *TunnelModeServer) Start() error {
if strings.Contains(err.Error(), "use of closed network connection") {
break
}
utils.Println(err)
lib.Println(err)
continue
}
go s.process(utils.NewConn(conn), s)
go s.process(lib.NewConn(conn), s)
}
return nil
}
//与客户端建立通道
func (s *TunnelModeServer) dealClient(c *utils.Conn, cnf *utils.Config, addr string, method string, rb []byte) error {
link := utils.NewLink(s.task.Client.GetId(), utils.CONN_TCP, addr, cnf.CompressEncode, cnf.CompressDecode, cnf.Crypt, c, s.task.Flow, nil, s.task.Client.Rate, nil)
func (s *TunnelModeServer) dealClient(c *lib.Conn, cnf *lib.Config, addr string, method string, rb []byte) error {
link := lib.NewLink(s.task.Client.GetId(), lib.CONN_TCP, addr, cnf.CompressEncode, cnf.CompressDecode, cnf.Crypt, c, s.task.Flow, nil, s.task.Client.Rate, nil)
if tunnel, err := s.bridge.SendLinkInfo(s.task.Client.Id, link); err != nil {
c.Close()
@@ -72,7 +72,7 @@ type WebServer struct {
//开始
func (s *WebServer) Start() error {
beego.BConfig.WebConfig.Session.SessionOn = true
utils.Println("web管理启动访问端口为", beego.AppConfig.String("httpport"))
lib.Println("web管理启动访问端口为", beego.AppConfig.String("httpport"))
beego.SetViewsPath(beego.AppPath + "/web/views")
beego.SetStaticPath("/static", beego.AppPath+"/web/static")
beego.Run()
@@ -96,10 +96,10 @@ func (s *HostServer) Start() error {
return nil
}
func NewHostServer(task *utils.Tunnel) *HostServer {
func NewHostServer(task *lib.Tunnel) *HostServer {
s := new(HostServer)
s.task = task
s.config = utils.DeepCopyConfig(task.Config)
s.config = lib.DeepCopyConfig(task.Config)
return s
}
@@ -108,10 +108,10 @@ func (s *HostServer) Close() error {
return nil
}
type process func(c *utils.Conn, s *TunnelModeServer) error
type process func(c *lib.Conn, s *TunnelModeServer) error
//tcp隧道模式
func ProcessTunnel(c *utils.Conn, s *TunnelModeServer) error {
func ProcessTunnel(c *lib.Conn, s *TunnelModeServer) error {
if !s.ResetConfig() {
c.Close()
return errors.New("流量超出")
@@ -120,7 +120,7 @@ func ProcessTunnel(c *utils.Conn, s *TunnelModeServer) error {
}
//http代理模式
func ProcessHttp(c *utils.Conn, s *TunnelModeServer) error {
func ProcessHttp(c *lib.Conn, s *TunnelModeServer) error {
if !s.ResetConfig() {
c.Close()
return errors.New("流量超出")

View File

@@ -2,7 +2,7 @@ package server
import (
"github.com/cnlh/nps/bridge"
"github.com/cnlh/nps/utils"
"github.com/cnlh/nps/lib"
"net"
"strings"
)
@@ -10,15 +10,15 @@ import (
type UdpModeServer struct {
server
listener *net.UDPConn
udpMap map[string]*utils.Conn
udpMap map[string]*lib.Conn
}
func NewUdpModeServer(bridge *bridge.Bridge, task *utils.Tunnel) *UdpModeServer {
func NewUdpModeServer(bridge *bridge.Bridge, task *lib.Tunnel) *UdpModeServer {
s := new(UdpModeServer)
s.bridge = bridge
s.udpMap = make(map[string]*utils.Conn)
s.udpMap = make(map[string]*lib.Conn)
s.task = task
s.config = utils.DeepCopyConfig(task.Config)
s.config = lib.DeepCopyConfig(task.Config)
return s
}
@@ -29,7 +29,7 @@ func (s *UdpModeServer) Start() error {
if err != nil {
return err
}
buf := utils.BufPoolUdp.Get().([]byte)
buf := lib.BufPoolUdp.Get().([]byte)
for {
n, addr, err := s.listener.ReadFromUDP(buf)
if err != nil {
@@ -47,7 +47,7 @@ func (s *UdpModeServer) Start() error {
}
func (s *UdpModeServer) process(addr *net.UDPAddr, data []byte) {
link := utils.NewLink(s.task.Client.GetId(), utils.CONN_UDP, s.task.Target, s.config.CompressEncode, s.config.CompressDecode, s.config.Crypt, nil, s.task.Flow, s.listener, s.task.Client.Rate, addr)
link := lib.NewLink(s.task.Client.GetId(), lib.CONN_UDP, s.task.Target, s.config.CompressEncode, s.config.CompressDecode, s.config.Crypt, nil, s.task.Flow, s.listener, s.task.Client.Rate, addr)
if tunnel, err := s.bridge.SendLinkInfo(s.task.Client.Id, link); err != nil {
return