mirror of https://github.com/ehang-io/nps
remove tcp keep-alive and update socks5 udp
parent
91c1ffc6ce
commit
2e5b1bd1e3
|
@ -158,7 +158,7 @@ func (s *TRPClient) newChan() {
|
||||||
|
|
||||||
func (s *TRPClient) handleChan(src net.Conn) {
|
func (s *TRPClient) handleChan(src net.Conn) {
|
||||||
lk, err := conn.NewConn(src).GetLinkInfo()
|
lk, err := conn.NewConn(src).GetLinkInfo()
|
||||||
if err != nil {
|
if err != nil || lk == nil {
|
||||||
src.Close()
|
src.Close()
|
||||||
logs.Error("get connection info from server error ", err)
|
logs.Error("get connection info from server error ", err)
|
||||||
return
|
return
|
||||||
|
@ -245,7 +245,6 @@ func (s *TRPClient) handleUdp(serverConn net.Conn) {
|
||||||
logs.Error("unpack data error", err.Error())
|
logs.Error("unpack data error", err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
raddr, err := net.ResolveUDPAddr("udp", udpData.Header.Addr.String())
|
raddr, err := net.ResolveUDPAddr("udp", udpData.Header.Addr.String())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logs.Error("build remote addr err", err.Error())
|
logs.Error("build remote addr err", err.Error())
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
|
"errors"
|
||||||
"html/template"
|
"html/template"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
@ -397,13 +398,61 @@ func GetExtFromPath(path string) string {
|
||||||
return string(re.Find([]byte(s[0])))
|
return string(re.Find([]byte(s[0])))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var externalIp string
|
||||||
|
|
||||||
func GetExternalIp() string {
|
func GetExternalIp() string {
|
||||||
|
if externalIp != "" {
|
||||||
|
return externalIp
|
||||||
|
}
|
||||||
resp, err := http.Get("http://myexternalip.com/raw")
|
resp, err := http.Get("http://myexternalip.com/raw")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
content, _ := ioutil.ReadAll(resp.Body)
|
content, _ := ioutil.ReadAll(resp.Body)
|
||||||
return string(content)
|
externalIp = string(content)
|
||||||
|
return externalIp
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetIntranetIp() (error, string) {
|
||||||
|
addrs, err := net.InterfaceAddrs()
|
||||||
|
if err != nil {
|
||||||
|
return nil, ""
|
||||||
|
}
|
||||||
|
for _, address := range addrs {
|
||||||
|
// 检查ip地址判断是否回环地址
|
||||||
|
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
|
||||||
|
if ipnet.IP.To4() != nil {
|
||||||
|
return nil, ipnet.IP.To4().String()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return errors.New("get intranet ip error"), ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func IsPublicIP(IP net.IP) bool {
|
||||||
|
if IP.IsLoopback() || IP.IsLinkLocalMulticast() || IP.IsLinkLocalUnicast() {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if ip4 := IP.To4(); ip4 != nil {
|
||||||
|
switch true {
|
||||||
|
case ip4[0] == 10:
|
||||||
|
return false
|
||||||
|
case ip4[0] == 172 && ip4[1] >= 16 && ip4[1] <= 31:
|
||||||
|
return false
|
||||||
|
case ip4[0] == 192 && ip4[1] == 168:
|
||||||
|
return false
|
||||||
|
default:
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetServerIpByClientIp(clientIp net.IP) string {
|
||||||
|
if IsPublicIP(clientIp) {
|
||||||
|
return GetExternalIp()
|
||||||
|
}
|
||||||
|
_, ip := GetIntranetIp()
|
||||||
|
return ip
|
||||||
}
|
}
|
||||||
|
|
|
@ -124,8 +124,8 @@ func (s *Conn) SetAlive(tp string) {
|
||||||
case *net.TCPConn:
|
case *net.TCPConn:
|
||||||
conn := s.Conn.(*net.TCPConn)
|
conn := s.Conn.(*net.TCPConn)
|
||||||
conn.SetReadDeadline(time.Time{})
|
conn.SetReadDeadline(time.Time{})
|
||||||
conn.SetKeepAlive(true)
|
//conn.SetKeepAlive(false)
|
||||||
conn.SetKeepAlivePeriod(time.Duration(2 * time.Second))
|
//conn.SetKeepAlivePeriod(time.Duration(2 * time.Second))
|
||||||
case *mux.PortConn:
|
case *mux.PortConn:
|
||||||
s.Conn.(*mux.PortConn).SetReadDeadline(time.Time{})
|
s.Conn.(*mux.PortConn).SetReadDeadline(time.Time{})
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,6 @@ package proxy
|
||||||
import (
|
import (
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
|
||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
@ -174,12 +173,7 @@ func (s *Sock5ModeServer) sendUdpReply(writeConn net.Conn, c net.Conn, rep uint8
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var serveExternalIp string
|
|
||||||
|
|
||||||
func (s *Sock5ModeServer) handleUDP(c net.Conn) {
|
func (s *Sock5ModeServer) handleUDP(c net.Conn) {
|
||||||
if serveExternalIp == "" {
|
|
||||||
serveExternalIp = common.GetExternalIp()
|
|
||||||
}
|
|
||||||
defer c.Close()
|
defer c.Close()
|
||||||
addrType := make([]byte, 1)
|
addrType := make([]byte, 1)
|
||||||
c.Read(addrType)
|
c.Read(addrType)
|
||||||
|
@ -206,7 +200,7 @@ func (s *Sock5ModeServer) handleUDP(c net.Conn) {
|
||||||
//读取端口
|
//读取端口
|
||||||
var port uint16
|
var port uint16
|
||||||
binary.Read(c, binary.BigEndian, &port)
|
binary.Read(c, binary.BigEndian, &port)
|
||||||
fmt.Println(host, string(port))
|
logs.Warn(host, string(port))
|
||||||
replyAddr, err := net.ResolveUDPAddr("udp", s.task.ServerIp+":0")
|
replyAddr, err := net.ResolveUDPAddr("udp", s.task.ServerIp+":0")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logs.Error("build local reply addr error", err)
|
logs.Error("build local reply addr error", err)
|
||||||
|
@ -219,9 +213,8 @@ func (s *Sock5ModeServer) handleUDP(c net.Conn) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// reply the local addr
|
// reply the local addr
|
||||||
s.sendUdpReply(c, reply, succeeded, serveExternalIp)
|
s.sendUdpReply(c, reply, succeeded, common.GetServerIpByClientIp(c.RemoteAddr().(*net.TCPAddr).IP))
|
||||||
defer reply.Close()
|
defer reply.Close()
|
||||||
|
|
||||||
// new a tunnel to client
|
// new a tunnel to client
|
||||||
link := conn.NewLink("udp", "", s.task.Client.Cnf.Crypt, s.task.Client.Cnf.Compress, c.RemoteAddr().String(), false)
|
link := conn.NewLink("udp", "", s.task.Client.Cnf.Crypt, s.task.Client.Cnf.Compress, c.RemoteAddr().String(), false)
|
||||||
target, err := s.bridge.SendLinkInfo(s.task.Client.Id, link, s.task)
|
target, err := s.bridge.SendLinkInfo(s.task.Client.Id, link, s.task)
|
||||||
|
|
Loading…
Reference in New Issue