mirror of
https://github.com/ehang-io/nps.git
synced 2025-09-02 03:16:53 +00:00
守护进程 负载均衡
This commit is contained in:
@@ -6,7 +6,7 @@ import (
|
||||
"github.com/astaxie/beego"
|
||||
"github.com/cnlh/easyProxy/bridge"
|
||||
"github.com/cnlh/easyProxy/utils"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httputil"
|
||||
"strconv"
|
||||
@@ -49,25 +49,38 @@ func (s *httpServer) Start() error {
|
||||
}
|
||||
|
||||
if s.httpPort > 0 {
|
||||
if !s.TestTcpPort(s.httpPort) {
|
||||
utils.Fatalln("http端口", s.httpPort, "被占用!")
|
||||
}
|
||||
http = s.NewServer(s.httpPort)
|
||||
go func() {
|
||||
log.Println("启动http监听,端口为", s.httpPort)
|
||||
utils.Println("启动http监听,端口为", s.httpPort)
|
||||
err := http.ListenAndServe()
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
utils.Fatalln(err)
|
||||
}
|
||||
}()
|
||||
}
|
||||
if s.httpsPort > 0 {
|
||||
if !s.TestTcpPort(s.httpsPort) {
|
||||
utils.Fatalln("https端口", s.httpsPort, "被占用!")
|
||||
}
|
||||
if !utils.FileExists(s.pemPath) {
|
||||
utils.Fatalf("ssl certFile文件%s不存在", s.pemPath)
|
||||
}
|
||||
if !utils.FileExists(s.keyPath) {
|
||||
utils.Fatalf("ssl keyFile文件%s不存在", s.keyPath)
|
||||
}
|
||||
https = s.NewServer(s.httpsPort)
|
||||
go func() {
|
||||
log.Println("启动https监听,端口为", s.httpsPort)
|
||||
utils.Println("启动https监听,端口为", s.httpsPort)
|
||||
err := https.ListenAndServeTLS(s.pemPath, s.keyPath)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
utils.Fatalln(err)
|
||||
}
|
||||
}()
|
||||
}
|
||||
startFinish <- true
|
||||
select {
|
||||
case <-s.stop:
|
||||
if http != nil {
|
||||
@@ -77,6 +90,7 @@ func (s *httpServer) Start() error {
|
||||
https.Close()
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -110,7 +124,7 @@ func (s *httpServer) process(c *utils.Conn, r *http.Request) {
|
||||
//首次获取conn
|
||||
if isConn {
|
||||
if host, err = GetInfoByHost(r.Host); err != nil {
|
||||
log.Printf("the host %s is not found !", r.Host)
|
||||
utils.Printf("the host %s is not found !", r.Host)
|
||||
break
|
||||
}
|
||||
//流量限制
|
||||
@@ -122,7 +136,7 @@ func (s *httpServer) process(c *utils.Conn, r *http.Request) {
|
||||
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.Target, host.Client.Cnf.CompressEncode, host.Client.Cnf.CompressDecode, host.Client.Cnf.Crypt, c, host.Flow, nil, host.Client.Rate, nil)
|
||||
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)
|
||||
if tunnel, err = s.bridge.SendLinkInfo(host.Client.Id, link); err != nil {
|
||||
break
|
||||
}
|
||||
@@ -166,3 +180,12 @@ func (s *httpServer) NewServer(port int) *http.Server {
|
||||
TLSNextProto: make(map[string]func(*http.Server, *tls.Conn, http.Handler)),
|
||||
}
|
||||
}
|
||||
|
||||
func (s *httpServer) TestTcpPort(port int) bool {
|
||||
l, err := net.ListenTCP("tcp", &net.TCPAddr{net.ParseIP("0.0.0.0"), port, ""})
|
||||
defer l.Close()
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
@@ -5,44 +5,58 @@ import (
|
||||
"github.com/cnlh/easyProxy/bridge"
|
||||
"github.com/cnlh/easyProxy/utils"
|
||||
"log"
|
||||
"os"
|
||||
"reflect"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
Bridge *bridge.Bridge
|
||||
RunList map[int]interface{} //运行中的任务
|
||||
CsvDb = utils.GetCsvDb()
|
||||
Bridge *bridge.Bridge
|
||||
RunList map[int]interface{} //运行中的任务
|
||||
CsvDb = utils.GetCsvDb()
|
||||
startFinish chan bool
|
||||
)
|
||||
|
||||
func init() {
|
||||
RunList = make(map[int]interface{})
|
||||
startFinish = make(chan bool)
|
||||
}
|
||||
|
||||
//从csv文件中恢复任务
|
||||
func InitFromCsv() {
|
||||
for _, v := range CsvDb.Tasks {
|
||||
if v.Status {
|
||||
log.Println("启动模式:", v.Mode, "监听端口:", v.TcpPort)
|
||||
utils.Println("启动模式:", v.Mode, "监听端口:", v.TcpPort)
|
||||
AddTask(v)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//start a new server
|
||||
func StartNewServer(bridgePort int, cnf *utils.Tunnel) {
|
||||
Bridge = bridge.NewTunnel(bridgePort, RunList)
|
||||
if err := Bridge.StartTunnel(); err != nil {
|
||||
log.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 {
|
||||
log.Println(err)
|
||||
func StartNewServer(bridgePort int, cnf *utils.Tunnel, test bool) {
|
||||
go func() {
|
||||
Bridge = bridge.NewTunnel(bridgePort, RunList)
|
||||
if err := Bridge.StartTunnel(); err != nil {
|
||||
utils.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)
|
||||
}
|
||||
} else {
|
||||
utils.Fatalln("启动模式不正确")
|
||||
}
|
||||
}()
|
||||
for {
|
||||
select {
|
||||
case <-startFinish:
|
||||
if test {
|
||||
log.Println("测试完成,未发现错误")
|
||||
os.Exit(0)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
log.Fatalln("启动模式不正确")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -98,7 +112,7 @@ func AddTask(t *utils.Tunnel) error {
|
||||
go func() {
|
||||
err := reflect.ValueOf(svr).MethodByName("Start").Call(nil)[0]
|
||||
if err.Interface() != nil {
|
||||
log.Println("客户端", t.Id, "启动失败,错误:", err)
|
||||
utils.Fatalln("服务端", t.Id, "启动失败,错误:", err)
|
||||
delete(RunList, t.Id)
|
||||
}
|
||||
}()
|
||||
|
@@ -6,7 +6,6 @@ import (
|
||||
"github.com/cnlh/easyProxy/bridge"
|
||||
"github.com/cnlh/easyProxy/utils"
|
||||
"io"
|
||||
"log"
|
||||
"net"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -66,7 +65,7 @@ func (s *Sock5ModeServer) handleRequest(c net.Conn) {
|
||||
_, err := io.ReadFull(c, header)
|
||||
|
||||
if err != nil {
|
||||
log.Println("illegal request", err)
|
||||
utils.Println("illegal request", err)
|
||||
c.Close()
|
||||
return
|
||||
}
|
||||
@@ -163,7 +162,7 @@ func (s *Sock5ModeServer) handleBind(c net.Conn) {
|
||||
|
||||
//udp
|
||||
func (s *Sock5ModeServer) handleUDP(c net.Conn) {
|
||||
log.Println("UDP Associate")
|
||||
utils.Println("UDP Associate")
|
||||
/*
|
||||
+----+------+------+----------+----------+----------+
|
||||
|RSV | FRAG | ATYP | DST.ADDR | DST.PORT | DATA |
|
||||
@@ -176,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
|
||||
log.Println("does not support fragmentation, drop")
|
||||
utils.Println("does not support fragmentation, drop")
|
||||
dummy := make([]byte, maxUDPPacketSize)
|
||||
c.Read(dummy)
|
||||
}
|
||||
@@ -188,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 {
|
||||
log.Println("negotiation err", err)
|
||||
utils.Println("negotiation err", err)
|
||||
c.Close()
|
||||
return
|
||||
}
|
||||
|
||||
if version := buf[0]; version != 5 {
|
||||
log.Println("only support socks5, request from: ", c.RemoteAddr())
|
||||
utils.Println("only support socks5, request from: ", c.RemoteAddr())
|
||||
c.Close()
|
||||
return
|
||||
}
|
||||
@@ -202,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 {
|
||||
log.Println("wrong method")
|
||||
utils.Println("wrong method")
|
||||
c.Close()
|
||||
return
|
||||
}
|
||||
@@ -211,7 +210,7 @@ func (s *Sock5ModeServer) handleConn(c net.Conn) {
|
||||
c.Write(buf)
|
||||
if err := s.Auth(c); err != nil {
|
||||
c.Close()
|
||||
log.Println("验证失败:", err)
|
||||
utils.Println("验证失败:", err)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
@@ -270,7 +269,7 @@ func (s *Sock5ModeServer) Start() error {
|
||||
if strings.Contains(err.Error(), "use of closed network connection") {
|
||||
break
|
||||
}
|
||||
log.Fatal("accept error: ", err)
|
||||
utils.Fatalln("accept error: ", err)
|
||||
}
|
||||
if !s.ResetConfig() {
|
||||
conn.Close()
|
||||
|
@@ -5,7 +5,6 @@ import (
|
||||
"github.com/astaxie/beego"
|
||||
"github.com/cnlh/easyProxy/bridge"
|
||||
"github.com/cnlh/easyProxy/utils"
|
||||
"log"
|
||||
"net"
|
||||
"strings"
|
||||
)
|
||||
@@ -39,7 +38,7 @@ func (s *TunnelModeServer) Start() error {
|
||||
if strings.Contains(err.Error(), "use of closed network connection") {
|
||||
break
|
||||
}
|
||||
log.Println(err)
|
||||
utils.Println(err)
|
||||
continue
|
||||
}
|
||||
go s.process(utils.NewConn(conn), s)
|
||||
@@ -71,12 +70,13 @@ type WebServer struct {
|
||||
}
|
||||
|
||||
//开始
|
||||
func (s *WebServer) Start() {
|
||||
func (s *WebServer) Start() error {
|
||||
beego.BConfig.WebConfig.Session.SessionOn = true
|
||||
log.Println("web管理启动,访问端口为", beego.AppConfig.String("httpport"))
|
||||
utils.Println("web管理启动,访问端口为", beego.AppConfig.String("httpport"))
|
||||
beego.SetViewsPath(beego.AppPath + "/web/views")
|
||||
beego.SetStaticPath("/static", beego.AppPath+"/web/static")
|
||||
beego.Run()
|
||||
return errors.New("web管理启动失败")
|
||||
}
|
||||
|
||||
//new
|
||||
|
Reference in New Issue
Block a user