Skip to content

Commit 4c94556

Browse files
authored
Update client.go
1 parent 843f0c5 commit 4c94556

1 file changed

Lines changed: 53 additions & 11 deletions

File tree

client.go

Lines changed: 53 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
package main
22

33
import (
4-
"encoding/binary"
54
"flag"
65
"fmt"
76
"net"
87
"os"
98
"time"
109
"github.com/sirupsen/logrus"
1110
"github.com/songgao/water"
11+
"github.com/xtaci/smux"
1212
"github.com/Azumi67/LocalTun_TCP/client"
1313
"github.com/Azumi67/LocalTun_TCP/smux_client"
14-
"github.com/Azumi67/LocalTun_TCP/tcp_no_delay_client"
1514
"github.com/Azumi67/LocalTun_TCP/heartbeat_client"
1615
"github.com/Azumi67/LocalTun_TCP/monitor_client"
16+
"github.com/Azumi67/LocalTun_TCP/hash"
17+
"github.com/Azumi67/LocalTun_TCP/tcp_no_delay_client"
1718
)
1819

1920
var log = logrus.New()
@@ -55,6 +56,7 @@ func main() {
5556
defer tun.Close()
5657

5758
tunclient.TunUp(tun, *tunIP, *serverTunIP, *subnetMask, *mtu, *tunName)
59+
5860
go monitorclient.monitor(*serverTunIP, *pingInterval, *serviceName)
5961

6062
for {
@@ -66,7 +68,7 @@ func main() {
6668
}
6769

6870
if *tcpNoDelay {
69-
tcpnodelayclient.noDelay(conn)
71+
tcpnodelayclient.SetTCPNoDelay(conn)
7072
}
7173

7274
defer conn.Close()
@@ -77,27 +79,67 @@ func main() {
7779
}
7880

7981
func handleServer(conn net.Conn, tun *water.Interface, secretKey string, verbose bool, useSmux bool, enableHeartbeat bool, heartbeatInterval int) {
80-
if _, err := conn.Write([]byte(secretKey)); err != nil {
81-
log.Warnf("Couldn't send authentication key: %v", err)
82+
challenge := make([]byte, 32)
83+
if _, err := conn.Read(challenge); err != nil {
84+
log.Warnf("Couldn't read challenge from server: %v", err)
85+
return
86+
}
87+
88+
hashedKey := hashclient.GenHash(challenge, secretKey)
89+
90+
if _, err := conn.Write(hashedKey); err != nil {
91+
log.Warnf("Couldn't send auth response: %v", err)
8292
return
8393
}
8494

8595
if enableHeartbeat {
86-
heartbeatclient.trueHeartbeat(conn, heartbeatInterval)
96+
go heartbeatclient.trueHeartbeat(conn, heartbeatInterval)
8797
}
8898

8999
if useSmux {
90-
clientmux.handleSmux(conn, tun, verbose)
100+
smuxclient.HandleSmux(conn, tun, verbose)
91101
return
92102
}
93103

94104
clientToTun := make(chan []byte, 100)
95105
tunToClient := make(chan []byte, 100)
96106

97-
go clientmux.FromServer(conn, tun, clientToTun, verbose)
98-
go clientmux.ToTun(tun, clientToTun, verbose)
99-
go clientmux.FromTun(tun, tunToClient, verbose)
100-
go clientmux.ToServer(conn, tunToClient, verbose)
107+
go fromServer(conn, tun, clientToTun, verbose)
108+
go tunclient.ToTun(tun, clientToTun, verbose)
109+
go tunclient.FromTun(tun, tunToClient, verbose)
110+
go toServer(conn, tunToClient, verbose)
101111

102112
select {}
103113
}
114+
115+
func fromServer(conn net.Conn, tun *water.Interface, clientToTun chan []byte, verbose bool) {
116+
for {
117+
pcktLength := make([]byte, 2)
118+
if _, err := conn.Read(pcktLength); err != nil {
119+
log.Warnf("Couldn't read packet length from server: %v", err)
120+
close(clientToTun)
121+
return
122+
}
123+
124+
length := binary.BigEndian.Uint16(pcktLength)
125+
buff := make([]byte, length)
126+
127+
_, err := tunclient.Data(conn, buff)
128+
if err != nil {
129+
log.Warnf("Couldn't read data from server: %v", err)
130+
close(clientToTun)
131+
return
132+
}
133+
134+
clientToTun <- buff
135+
}
136+
}
137+
138+
func toServer(conn net.Conn, tunToClient chan []byte, verbose bool) {
139+
for packet := range tunToClient {
140+
_, err := conn.Write(packet)
141+
if err != nil {
142+
log.Warnf("Couldn't write to server: %v", err)
143+
}
144+
}
145+
}

0 commit comments

Comments
 (0)