|
|
@ -27,8 +27,8 @@ func init() {
|
|
|
|
|
|
|
|
|
|
|
|
natccmd.Flags().BoolP("tcp", "t", false, "TCP穿透")
|
|
|
|
natccmd.Flags().BoolP("tcp", "t", false, "TCP穿透")
|
|
|
|
natccmd.Flags().BoolP("udp", "u", false, "UDP穿透")
|
|
|
|
natccmd.Flags().BoolP("udp", "u", false, "UDP穿透")
|
|
|
|
natccmd.Flags().StringP("port", "p", "1127", "转发穿透端口")
|
|
|
|
natccmd.Flags().IntP("port", "p", 1127, "转发穿透端口")
|
|
|
|
natccmd.Flags().StringP("key", "k", "1127", "认证密码")
|
|
|
|
natccmd.Flags().StringP("key", "k", "sakura", "认证密码")
|
|
|
|
natccmd.Flags().StringP("address", "a", "127.0.0.1", "转发穿透地址")
|
|
|
|
natccmd.Flags().StringP("address", "a", "127.0.0.1", "转发穿透地址")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -130,7 +130,7 @@ var natccmd = &cobra.Command{
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
t, _ := cmd.Flags().GetBool("tcp")
|
|
|
|
t, _ := cmd.Flags().GetBool("tcp")
|
|
|
|
u, _ := cmd.Flags().GetBool("udp")
|
|
|
|
u, _ := cmd.Flags().GetBool("udp")
|
|
|
|
p, _ := cmd.Flags().GetString("port")
|
|
|
|
p, _ := cmd.Flags().GetInt("port")
|
|
|
|
k, _ := cmd.Flags().GetString("key")
|
|
|
|
k, _ := cmd.Flags().GetString("key")
|
|
|
|
a, _ := cmd.Flags().GetString("address")
|
|
|
|
a, _ := cmd.Flags().GetString("address")
|
|
|
|
/*udplocal, err := net.ResolveUDPAddr("udp", a+":"+p)
|
|
|
|
/*udplocal, err := net.ResolveUDPAddr("udp", a+":"+p)
|
|
|
@ -152,13 +152,13 @@ var natccmd = &cobra.Command{
|
|
|
|
fmt.Println(err)
|
|
|
|
fmt.Println(err)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tcpforward, err := net.ResolveTCPAddr("tcp", a+":"+p)
|
|
|
|
tcpforward, err := net.ResolveTCPAddr("tcp", a+":"+strconv.Itoa(p))
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
fmt.Println(err)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
udprmt, _ := net.ResolveUDPAddr("udp", args[0])
|
|
|
|
udprmt, _ := net.ResolveUDPAddr("udp", args[0])
|
|
|
|
udpforward, _ := net.ResolveUDPAddr("udp", a+":"+p)
|
|
|
|
udpforward, _ := net.ResolveUDPAddr("udp", a+":"+strconv.Itoa(p))
|
|
|
|
if t {
|
|
|
|
if t {
|
|
|
|
go TcpNatClient(tcprmt, tcpforward, k)
|
|
|
|
go TcpNatClient(tcprmt, tcpforward, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -181,6 +181,7 @@ func UdpNatClient(udpremote, udpforward *net.UDPAddr, pwd string) {
|
|
|
|
var trueconn *net.UDPConn
|
|
|
|
var trueconn *net.UDPConn
|
|
|
|
var err error
|
|
|
|
var err error
|
|
|
|
ismain := false
|
|
|
|
ismain := false
|
|
|
|
|
|
|
|
var lastcheck int64
|
|
|
|
dialudp := func(udpaddr *net.UDPAddr) (*net.UDPConn, error) {
|
|
|
|
dialudp := func(udpaddr *net.UDPAddr) (*net.UDPConn, error) {
|
|
|
|
trueconn, err := net.DialUDP("udp", nil, udpaddr)
|
|
|
|
trueconn, err := net.DialUDP("udp", nil, udpaddr)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
@ -188,63 +189,94 @@ func UdpNatClient(udpremote, udpforward *net.UDPAddr, pwd string) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return trueconn, nil
|
|
|
|
return trueconn, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
|
|
|
|
if err := recover(); err != nil {
|
|
|
|
|
|
|
|
fmt.Println("PANIC:", err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}()
|
|
|
|
for {
|
|
|
|
for {
|
|
|
|
for trueconn, err = dialudp(udpremote); err != nil; trueconn, err = dialudp(udpremote) {
|
|
|
|
trueconn, err = dialudp(udpremote)
|
|
|
|
fmt.Println(err)
|
|
|
|
if err != nil || trueconn == nil {
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
|
|
|
|
fmt.Println(err)
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("UDP远端服务器连接成功" + trueconn.RemoteAddr().String())
|
|
|
|
fmt.Println("远端服务器连接成功" + trueconn.RemoteAddr().String())
|
|
|
|
|
|
|
|
trueconn.Write([]byte(pwd))
|
|
|
|
trueconn.Write([]byte(pwd))
|
|
|
|
go func() {
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
for !ismain {
|
|
|
|
time.Sleep(time.Second * 5)
|
|
|
|
time.Sleep(time.Second * 2)
|
|
|
|
if !ismain {
|
|
|
|
if trueconn != nil {
|
|
|
|
|
|
|
|
if ismain {
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
_, err := trueconn.Write([]byte(pwd))
|
|
|
|
_, err := trueconn.Write([]byte(pwd))
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
fmt.Println(err)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}()
|
|
|
|
for {
|
|
|
|
go func() {
|
|
|
|
var conn1, conn2 *net.UDPConn
|
|
|
|
for {
|
|
|
|
buf := make([]byte, 7)
|
|
|
|
if trueconn == nil {
|
|
|
|
n, err := trueconn.Read(buf)
|
|
|
|
|
|
|
|
if n != 7 || err != nil {
|
|
|
|
|
|
|
|
fmt.Println(err)
|
|
|
|
|
|
|
|
trueconn.Close()
|
|
|
|
|
|
|
|
ismain = false
|
|
|
|
|
|
|
|
break
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
switch string(buf) {
|
|
|
|
|
|
|
|
case "yesyesi":
|
|
|
|
|
|
|
|
ismain = true
|
|
|
|
|
|
|
|
fmt.Println("主节点认证成功")
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
|
|
|
|
for {
|
|
|
|
|
|
|
|
time.Sleep(time.Second * 2)
|
|
|
|
|
|
|
|
trueconn.Write([]byte("vicregs"))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
case "newconn":
|
|
|
|
|
|
|
|
if !ismain {
|
|
|
|
|
|
|
|
trueconn.Close()
|
|
|
|
|
|
|
|
break
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for conn1, err = dialudp(udpforward); err != nil; conn1, err = dialudp(udpforward) {
|
|
|
|
var conn1, conn2 *net.UDPConn
|
|
|
|
time.Sleep(time.Second * 2)
|
|
|
|
buf := make([]byte, 7)
|
|
|
|
|
|
|
|
n, err := trueconn.Read(buf)
|
|
|
|
|
|
|
|
if n != 7 || err != nil {
|
|
|
|
|
|
|
|
fmt.Println(err)
|
|
|
|
|
|
|
|
trueconn = nil
|
|
|
|
|
|
|
|
ismain = false
|
|
|
|
|
|
|
|
fmt.Println("UDP下线01")
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fmt.Println("已连接转发远端:" + conn1.RemoteAddr().String())
|
|
|
|
switch string(buf) {
|
|
|
|
for conn2, err = dialudp(udpremote); err != nil; conn2, err = dialudp(udpremote) {
|
|
|
|
case "yesyesi":
|
|
|
|
time.Sleep(time.Second * 2)
|
|
|
|
ismain = true
|
|
|
|
|
|
|
|
fmt.Println("UDP主节点认证成功")
|
|
|
|
|
|
|
|
lastcheck = time.Now().Unix()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
|
|
|
|
for {
|
|
|
|
|
|
|
|
time.Sleep(time.Second * 2)
|
|
|
|
|
|
|
|
if time.Now().Unix()-lastcheck > 4 {
|
|
|
|
|
|
|
|
ismain = false
|
|
|
|
|
|
|
|
trueconn = nil
|
|
|
|
|
|
|
|
fmt.Println("UDP下线02")
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
case "vicregs":
|
|
|
|
|
|
|
|
if trueconn != nil {
|
|
|
|
|
|
|
|
trueconn.Write([]byte("vicregs"))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
lastcheck = time.Now().Unix()
|
|
|
|
|
|
|
|
case "newconn":
|
|
|
|
|
|
|
|
if !ismain {
|
|
|
|
|
|
|
|
trueconn.Close()
|
|
|
|
|
|
|
|
break
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
for conn1, err = dialudp(udpforward); err != nil; conn1, err = dialudp(udpforward) {
|
|
|
|
|
|
|
|
time.Sleep(time.Second * 2)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("UDP已连接转发远端:" + conn1.RemoteAddr().String())
|
|
|
|
|
|
|
|
for conn2, err = dialudp(udpremote); err != nil; conn2, err = dialudp(udpremote) {
|
|
|
|
|
|
|
|
time.Sleep(time.Second * 2)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
conn2.Write([]byte("v%2^f&K"))
|
|
|
|
|
|
|
|
fmt.Println("UDP已连接主控远端:" + conn2.RemoteAddr().String())
|
|
|
|
|
|
|
|
go UdpCopy(conn1, conn2)
|
|
|
|
|
|
|
|
go UdpCopy(conn2, conn1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
conn2.Write([]byte("v%2^f&K"))
|
|
|
|
|
|
|
|
fmt.Println("已连接主控远端:" + conn2.RemoteAddr().String())
|
|
|
|
|
|
|
|
go UdpCopy(conn1, conn2)
|
|
|
|
|
|
|
|
go UdpCopy(conn2, conn1)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
for ismain || trueconn != nil {
|
|
|
|
|
|
|
|
time.Sleep(time.Second * 4)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ismain = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -252,6 +284,7 @@ func TcpNatClient(tcpremote, tcpforward *net.TCPAddr, pwd string) {
|
|
|
|
var trueconn *net.TCPConn
|
|
|
|
var trueconn *net.TCPConn
|
|
|
|
var err error
|
|
|
|
var err error
|
|
|
|
ismain := false
|
|
|
|
ismain := false
|
|
|
|
|
|
|
|
var lastcheck int64
|
|
|
|
dialtcp := func(tcpaddr *net.TCPAddr) (*net.TCPConn, error) {
|
|
|
|
dialtcp := func(tcpaddr *net.TCPAddr) (*net.TCPConn, error) {
|
|
|
|
trueconn, err := net.DialTCP("tcp", nil, tcpaddr)
|
|
|
|
trueconn, err := net.DialTCP("tcp", nil, tcpaddr)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
@ -259,57 +292,103 @@ func TcpNatClient(tcpremote, tcpforward *net.TCPAddr, pwd string) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return trueconn, nil
|
|
|
|
return trueconn, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
|
|
|
|
if err := recover(); err != nil {
|
|
|
|
|
|
|
|
fmt.Println("PANIC:", err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}()
|
|
|
|
for {
|
|
|
|
for {
|
|
|
|
for trueconn, err = dialtcp(tcpremote); err != nil; trueconn, err = dialtcp(tcpremote) {
|
|
|
|
trueconn, err = dialtcp(tcpremote)
|
|
|
|
|
|
|
|
if err != nil || trueconn == nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
fmt.Println(err)
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("TCP远端服务器连接成功" + trueconn.RemoteAddr().String())
|
|
|
|
|
|
|
|
n, err := trueconn.Write([]byte(pwd))
|
|
|
|
|
|
|
|
if err != nil || n == 0 {
|
|
|
|
|
|
|
|
fmt.Println(err)
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fmt.Println("远端服务器连接成功" + trueconn.RemoteAddr().String())
|
|
|
|
|
|
|
|
trueconn.Write([]byte(pwd))
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
for {
|
|
|
|
time.Sleep(time.Second * 5)
|
|
|
|
time.Sleep(time.Second * 2)
|
|
|
|
if !ismain {
|
|
|
|
if !ismain {
|
|
|
|
_, err := trueconn.Write([]byte(pwd))
|
|
|
|
if trueconn != nil {
|
|
|
|
if err != nil {
|
|
|
|
_, err := trueconn.Write([]byte(pwd))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}()
|
|
|
|
for {
|
|
|
|
go func() {
|
|
|
|
var conn1, conn2 *net.TCPConn
|
|
|
|
for {
|
|
|
|
buf := make([]byte, 7)
|
|
|
|
if trueconn == nil {
|
|
|
|
n, err := trueconn.Read(buf)
|
|
|
|
|
|
|
|
if n != 7 || err != nil {
|
|
|
|
|
|
|
|
fmt.Println(err)
|
|
|
|
|
|
|
|
trueconn.Close()
|
|
|
|
|
|
|
|
ismain = false
|
|
|
|
|
|
|
|
break
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
switch string(buf) {
|
|
|
|
|
|
|
|
case "yesyesi":
|
|
|
|
|
|
|
|
ismain = true
|
|
|
|
|
|
|
|
fmt.Println("主节点认证成功")
|
|
|
|
|
|
|
|
case "newconn":
|
|
|
|
|
|
|
|
if !ismain {
|
|
|
|
|
|
|
|
trueconn.Close()
|
|
|
|
|
|
|
|
break
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for conn1, err = dialtcp(tcpforward); err != nil; conn1, err = dialtcp(tcpforward) {
|
|
|
|
var conn1, conn2 *net.TCPConn
|
|
|
|
time.Sleep(time.Second * 2)
|
|
|
|
buf := make([]byte, 7)
|
|
|
|
|
|
|
|
n, err := trueconn.Read(buf)
|
|
|
|
|
|
|
|
if n != 7 || err != nil {
|
|
|
|
|
|
|
|
fmt.Println("TCP下线01")
|
|
|
|
|
|
|
|
fmt.Println(err)
|
|
|
|
|
|
|
|
trueconn = nil
|
|
|
|
|
|
|
|
ismain = false
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fmt.Println("已连接转发远端:" + conn1.RemoteAddr().String())
|
|
|
|
switch string(buf) {
|
|
|
|
for conn2, err = dialtcp(tcpremote); err != nil; conn2, err = dialtcp(tcpremote) {
|
|
|
|
case "yesyesi":
|
|
|
|
time.Sleep(time.Second * 2)
|
|
|
|
ismain = true
|
|
|
|
|
|
|
|
fmt.Println("TCP主节点认证成功")
|
|
|
|
|
|
|
|
lastcheck = time.Now().Unix()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
|
|
|
|
for {
|
|
|
|
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
|
|
|
|
if time.Now().Unix()-lastcheck > 4 {
|
|
|
|
|
|
|
|
fmt.Println("TCP下线02")
|
|
|
|
|
|
|
|
ismain = false
|
|
|
|
|
|
|
|
trueconn = nil
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
case "vicb612":
|
|
|
|
|
|
|
|
lastcheck = time.Now().Unix()
|
|
|
|
|
|
|
|
if trueconn != nil {
|
|
|
|
|
|
|
|
trueconn.Write([]byte("vicb612"))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
case "newconn":
|
|
|
|
|
|
|
|
if !ismain {
|
|
|
|
|
|
|
|
trueconn.Close()
|
|
|
|
|
|
|
|
trueconn = nil
|
|
|
|
|
|
|
|
fmt.Println("TCP下线03")
|
|
|
|
|
|
|
|
break
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
for conn1, err = dialtcp(tcpforward); err != nil; conn1, err = dialtcp(tcpforward) {
|
|
|
|
|
|
|
|
time.Sleep(time.Second * 2)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("TCP已连接转发远端:" + conn1.RemoteAddr().String())
|
|
|
|
|
|
|
|
for conn2, err = dialtcp(tcpremote); err != nil; conn2, err = dialtcp(tcpremote) {
|
|
|
|
|
|
|
|
time.Sleep(time.Second * 2)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
conn2.Write([]byte("v%2^f&K"))
|
|
|
|
|
|
|
|
fmt.Println("TCP已连接主控远端:" + conn2.RemoteAddr().String())
|
|
|
|
|
|
|
|
go TcpCopy(conn1, conn2)
|
|
|
|
|
|
|
|
go TcpCopy(conn2, conn1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
conn2.Write([]byte("v%2^f&K"))
|
|
|
|
|
|
|
|
fmt.Println("已连接主控远端:" + conn2.RemoteAddr().String())
|
|
|
|
|
|
|
|
go TcpCopy(conn1, conn2)
|
|
|
|
|
|
|
|
go TcpCopy(conn2, conn1)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
for ismain || trueconn != nil {
|
|
|
|
|
|
|
|
time.Sleep(time.Second * 4)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ismain = false
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -362,21 +441,33 @@ func UdpNatServer(udplocal *net.UDPAddr, pwd string) {
|
|
|
|
fmt.Println(err)
|
|
|
|
fmt.Println(err)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
udpconnlist := []string{}
|
|
|
|
udpool := make(map[string]*udpnat)
|
|
|
|
udpool := make(map[string]*udpnat)
|
|
|
|
udpdial := make(map[string]*net.UDPAddr)
|
|
|
|
udpdial := make(map[string]*net.UDPAddr)
|
|
|
|
fmt.Println("UDP已建立监听:" + udplistener.LocalAddr().String())
|
|
|
|
fmt.Println("UDP已建立监听:" + udplistener.LocalAddr().String())
|
|
|
|
|
|
|
|
removeudplist := func(str string) {
|
|
|
|
|
|
|
|
var tmp []string
|
|
|
|
|
|
|
|
for _, v := range udpconnlist {
|
|
|
|
|
|
|
|
if v != str {
|
|
|
|
|
|
|
|
tmp = append(tmp, str)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
udpconnlist = tmp
|
|
|
|
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
for {
|
|
|
|
time.Sleep(time.Second * 300)
|
|
|
|
time.Sleep(time.Second * 20)
|
|
|
|
pool := []string{}
|
|
|
|
pool := []string{}
|
|
|
|
for k, v := range udpool {
|
|
|
|
for k, v := range udpool {
|
|
|
|
if time.Now().Unix()-v.LastTrans > 600 {
|
|
|
|
if time.Now().Unix()-v.LastTrans > 30 {
|
|
|
|
pool = append(pool, k)
|
|
|
|
pool = append(pool, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, v := range pool {
|
|
|
|
for _, v := range pool {
|
|
|
|
if udpool[v].Married {
|
|
|
|
if udpool[v].Married {
|
|
|
|
delete(udpdial, udpool[v].Conn.String())
|
|
|
|
delete(udpdial, udpool[v].Conn.String())
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
removeudplist(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
delete(udpool, v)
|
|
|
|
delete(udpool, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -390,17 +481,22 @@ func UdpNatServer(udplocal *net.UDPAddr, pwd string) {
|
|
|
|
if errp != nil {
|
|
|
|
if errp != nil {
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if string(buf[0:len(pwd)]) == pwd {
|
|
|
|
if string(buf[0:len(pwd)]) == pwd && n == len(pwd) {
|
|
|
|
isconn = true
|
|
|
|
isconn = true
|
|
|
|
trueconn = conn
|
|
|
|
trueconn = conn
|
|
|
|
udplistener.WriteToUDP([]byte("yesyesi"), trueconn)
|
|
|
|
udplistener.WriteToUDP([]byte("yesyesi"), trueconn)
|
|
|
|
fmt.Println("穿透客户端已注册")
|
|
|
|
fmt.Println("UDP穿透客户端已注册")
|
|
|
|
|
|
|
|
udpool = make(map[string]*udpnat)
|
|
|
|
|
|
|
|
udpdial = make(map[string]*net.UDPAddr)
|
|
|
|
|
|
|
|
udpconnlist = []string{}
|
|
|
|
lastcheck = time.Now().Unix()
|
|
|
|
lastcheck = time.Now().Unix()
|
|
|
|
go func() {
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
for {
|
|
|
|
time.Sleep(time.Second * 5)
|
|
|
|
time.Sleep(time.Second * 2)
|
|
|
|
if time.Now().Unix()-lastcheck > 4 {
|
|
|
|
go udplistener.WriteToUDP([]byte("vicregs"), trueconn)
|
|
|
|
fmt.Println("穿透客户端已下线")
|
|
|
|
if time.Now().Unix()-lastcheck > 6 {
|
|
|
|
|
|
|
|
fmt.Println("UDP穿透客户端已下线")
|
|
|
|
|
|
|
|
fmt.Println("UDP下线01")
|
|
|
|
isconn = false
|
|
|
|
isconn = false
|
|
|
|
break
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -414,15 +510,21 @@ func UdpNatServer(udplocal *net.UDPAddr, pwd string) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if string(buf[0:7]) == "v%2^f&K" {
|
|
|
|
if string(buf[0:7]) == "v%2^f&K" {
|
|
|
|
fmt.Println("穿透客户端已建立新连接")
|
|
|
|
fmt.Println("穿透客户端已建立新连接")
|
|
|
|
for k, v := range udpool {
|
|
|
|
if len(udpconnlist) != 0 {
|
|
|
|
if !v.Married {
|
|
|
|
if v, ok := udpool[udpconnlist[0]]; ok {
|
|
|
|
v.Conn = conn
|
|
|
|
if !v.Married {
|
|
|
|
udplistener.WriteToUDP(v.Msg, conn)
|
|
|
|
v.Conn = conn
|
|
|
|
v.Msg = []byte{}
|
|
|
|
udplistener.WriteToUDP(v.Msg, conn)
|
|
|
|
udpaddr, _ := net.ResolveUDPAddr("udp", k)
|
|
|
|
v.Msg = []byte{}
|
|
|
|
udpdial[conn.String()] = udpaddr
|
|
|
|
udpaddr, _ := net.ResolveUDPAddr("udp", udpconnlist[0])
|
|
|
|
v.Married = true
|
|
|
|
udpdial[conn.String()] = udpaddr
|
|
|
|
break
|
|
|
|
v.Married = true
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(udpconnlist) == 1 {
|
|
|
|
|
|
|
|
udpconnlist = []string{}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
udpconnlist = udpconnlist[1:]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
return
|
|
|
@ -468,9 +570,11 @@ func UdpNatServer(udplocal *net.UDPAddr, pwd string) {
|
|
|
|
Married: false,
|
|
|
|
Married: false,
|
|
|
|
LastTrans: time.Now().Unix(),
|
|
|
|
LastTrans: time.Now().Unix(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
udpconnlist = append(udpconnlist, conn.String())
|
|
|
|
_, err := udplistener.WriteToUDP([]byte("newconn"), trueconn)
|
|
|
|
_, err := udplistener.WriteToUDP([]byte("newconn"), trueconn)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("穿透客户端已下线")
|
|
|
|
fmt.Println("UDP穿透客户端已下线")
|
|
|
|
|
|
|
|
fmt.Println("UDP下线02")
|
|
|
|
isconn = false
|
|
|
|
isconn = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -482,6 +586,7 @@ func UdpNatServer(udplocal *net.UDPAddr, pwd string) {
|
|
|
|
|
|
|
|
|
|
|
|
func TcpNatServer(tcplocal *net.TCPAddr, pwd string) {
|
|
|
|
func TcpNatServer(tcplocal *net.TCPAddr, pwd string) {
|
|
|
|
var trueconn *net.TCPConn
|
|
|
|
var trueconn *net.TCPConn
|
|
|
|
|
|
|
|
lastcheck := int64(0)
|
|
|
|
isconn := false
|
|
|
|
isconn := false
|
|
|
|
tcplistener, err := net.ListenTCP("tcp", tcplocal)
|
|
|
|
tcplistener, err := net.ListenTCP("tcp", tcplocal)
|
|
|
|
defer tcplistener.Close()
|
|
|
|
defer tcplistener.Close()
|
|
|
@ -509,32 +614,53 @@ func TcpNatServer(tcplocal *net.TCPAddr, pwd string) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
time.Sleep(time.Millisecond * 600)
|
|
|
|
time.Sleep(time.Second * 2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
go func() {
|
|
|
|
if !isconn {
|
|
|
|
if !isconn {
|
|
|
|
buf := make([]byte, len(pwd))
|
|
|
|
buf := make([]byte, len(pwd)+1)
|
|
|
|
n, err := conn.Read(buf)
|
|
|
|
n, err := conn.Read(buf)
|
|
|
|
if n != len(pwd) {
|
|
|
|
if n != len(pwd) || err != nil {
|
|
|
|
conn.Close()
|
|
|
|
conn.Close()
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
if string(buf[0:len(pwd)]) == pwd {
|
|
|
|
conn.Close()
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if string(buf) == pwd {
|
|
|
|
|
|
|
|
isconn = true
|
|
|
|
isconn = true
|
|
|
|
trueconn = conn
|
|
|
|
trueconn = conn
|
|
|
|
conn.Write([]byte("yesyesi"))
|
|
|
|
conn.Write([]byte("yesyesi"))
|
|
|
|
fmt.Println("穿透客户端已注册")
|
|
|
|
fmt.Println("TCP穿透客户端已注册")
|
|
|
|
|
|
|
|
lastcheck = time.Now().Unix()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
|
|
|
|
for isconn && trueconn != nil {
|
|
|
|
|
|
|
|
buf := make([]byte, 7)
|
|
|
|
|
|
|
|
n, err := trueconn.Read(buf)
|
|
|
|
|
|
|
|
if n != 7 || err != nil {
|
|
|
|
|
|
|
|
isconn = false
|
|
|
|
|
|
|
|
trueconn = nil
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if string(buf) == "vicb612" {
|
|
|
|
|
|
|
|
lastcheck = time.Now().Unix()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
for {
|
|
|
|
time.Sleep(time.Second * 2)
|
|
|
|
time.Sleep(time.Second * 2)
|
|
|
|
_, err := trueconn.Write([]byte("vicb612"))
|
|
|
|
if trueconn != nil {
|
|
|
|
if err != nil {
|
|
|
|
_, err := trueconn.Write([]byte("vicb612"))
|
|
|
|
trueconn.Close()
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
trueconn.Close()
|
|
|
|
|
|
|
|
trueconn = nil
|
|
|
|
|
|
|
|
isconn = false
|
|
|
|
|
|
|
|
fmt.Println("TCP下线01")
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if time.Now().Unix()-lastcheck > 4 {
|
|
|
|
|
|
|
|
fmt.Println("TCP下线02")
|
|
|
|
|
|
|
|
trueconn = nil
|
|
|
|
isconn = false
|
|
|
|
isconn = false
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -548,14 +674,11 @@ func TcpNatServer(tcplocal *net.TCPAddr, pwd string) {
|
|
|
|
if isconn {
|
|
|
|
if isconn {
|
|
|
|
buf := make([]byte, 7)
|
|
|
|
buf := make([]byte, 7)
|
|
|
|
n, err := conn.Read(buf)
|
|
|
|
n, err := conn.Read(buf)
|
|
|
|
if n != 7 {
|
|
|
|
if n != 7 || err != nil {
|
|
|
|
conn.Close()
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
conn.Close()
|
|
|
|
conn.Close()
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if string(buf) == "v%2^f&K" {
|
|
|
|
if string(buf) == "v%2^f&K" {
|
|
|
|
fmt.Println("穿透客户端已建立新连接")
|
|
|
|
fmt.Println("穿透客户端已建立新连接")
|
|
|
|
if len(waitconn) != 0 {
|
|
|
|
if len(waitconn) != 0 {
|
|
|
@ -574,10 +697,17 @@ func TcpNatServer(tcplocal *net.TCPAddr, pwd string) {
|
|
|
|
fmt.Println("链接已加入等待列表")
|
|
|
|
fmt.Println("链接已加入等待列表")
|
|
|
|
tcpnats := tcpnat{Msg: buf, Conn: conn, Date: time.Now().Unix()}
|
|
|
|
tcpnats := tcpnat{Msg: buf, Conn: conn, Date: time.Now().Unix()}
|
|
|
|
waitconn = append(waitconn, tcpnats)
|
|
|
|
waitconn = append(waitconn, tcpnats)
|
|
|
|
|
|
|
|
if trueconn == nil {
|
|
|
|
|
|
|
|
isconn = false
|
|
|
|
|
|
|
|
fmt.Println("TCP下线04")
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
_, err := trueconn.Write([]byte("newconn"))
|
|
|
|
_, err := trueconn.Write([]byte("newconn"))
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("穿透客户端已下线")
|
|
|
|
fmt.Println("穿透客户端已下线")
|
|
|
|
|
|
|
|
fmt.Println("TCP下线03")
|
|
|
|
trueconn.Close()
|
|
|
|
trueconn.Close()
|
|
|
|
|
|
|
|
trueconn = nil
|
|
|
|
isconn = false
|
|
|
|
isconn = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|