@@ -12,17 +12,32 @@ namespace horizon
12
12
public class HorizonClient
13
13
{
14
14
public IoManager ioManager ;
15
- private string tunnelDestination ;
16
- private int tunnelDestinationPort ;
17
- private Uri horizonHost ;
15
+ private string _tunnelDestination ;
16
+ private int _tunnelDestinationPort ;
17
+ private Uri _horizonHost ;
18
18
private bool stopFlag = false ;
19
- private Socket localSock ;
19
+ private Socket _localSock ;
20
+ private string _userId ;
21
+ private string _userToken ;
20
22
21
- public HorizonClient ( Uri horizonHost , string destination , int port )
23
+ public HorizonClient ( Uri horizonHost , string destination , int port , string userId , string userToken )
22
24
{
23
- this . horizonHost = horizonHost ;
24
- tunnelDestination = destination ;
25
- tunnelDestinationPort = port ;
25
+ _userId = userId ;
26
+ _userToken = userToken ;
27
+ _horizonHost = horizonHost ;
28
+ _tunnelDestination = destination ;
29
+ _tunnelDestinationPort = port ;
30
+ }
31
+ /// <summary>
32
+ /// Pings the server to check credentials and latency
33
+ /// </summary>
34
+ /// <returns></returns>
35
+ public PingResult Ping ( )
36
+ {
37
+ WStream client = new WStream ( ) ;
38
+ var connection = client . Connect ( _horizonHost , CancellationToken . None ) ;
39
+ var s = AuthenticatePing ( connection , out var latency ) ;
40
+ return new PingResult ( ) { Latency = latency , Success = s } ;
26
41
}
27
42
28
43
/// <summary>
@@ -32,13 +47,13 @@ public HorizonClient(Uri horizonHost, string destination, int port)
32
47
/// <param name="userId"></param>
33
48
/// <param name="token"></param>
34
49
/// <param name="ioConfig"></param>
35
- public void OpenTunnel ( EndPoint localBinding , string userId , string token , HorizonOptions ioConfig )
50
+ public void OpenTunnel ( EndPoint localBinding , HorizonOptions ioConfig )
36
51
{
37
52
ioManager = new IoManager ( ioConfig ) ;
38
- localSock = new Socket ( AddressFamily . InterNetwork , SocketType . Stream , ProtocolType . Tcp ) ;
39
- localSock . Bind ( localBinding ) ;
40
- localSock . Listen ( 1000 ) ;
41
- new Thread ( ( ) => SocketListener ( userId , token ) ) . Start ( ) ;
53
+ _localSock = new Socket ( AddressFamily . InterNetwork , SocketType . Stream , ProtocolType . Tcp ) ;
54
+ _localSock . Bind ( localBinding ) ;
55
+ _localSock . Listen ( 1000 ) ;
56
+ new Thread ( SocketListener ) . Start ( ) ;
42
57
}
43
58
44
59
/// <summary>
@@ -49,49 +64,82 @@ public void CloseTunnel()
49
64
ioManager . Stop ( ) ;
50
65
}
51
66
52
- private void SocketListener ( string userId , string token )
67
+ private void SocketListener ( )
53
68
{
54
69
while ( ! stopFlag )
55
70
{
56
- var sock = localSock . Accept ( ) ;
57
- Connect ( userId , token , sock ) ;
71
+ try
72
+ {
73
+ var sock = _localSock . Accept ( ) ;
74
+ Connect ( sock ) ;
75
+ }
76
+ catch
77
+ {
78
+ "Failed to accept local client" . Log ( Logger . LoggingLevel . Severe ) ;
79
+ }
58
80
}
59
81
}
60
82
61
- private void Connect ( string userId , string token , Socket _localSock )
83
+ private void Connect ( Socket localSock )
62
84
{
63
- WStream client = new WStream ( ) ;
64
- var connection = client . Connect ( horizonHost , CancellationToken . None ) ;
65
- if ( Authenticate ( userId , token , connection , out var req ) )
85
+ try
66
86
{
67
- ioManager . AddIoConnection ( connection , _localSock , req ) ;
87
+ WStream client = new WStream ( ) ;
88
+ var connection = client . Connect ( _horizonHost , CancellationToken . None ) ;
89
+ if ( Authenticate ( connection , out var req ) )
90
+ {
91
+ ioManager . AddIoConnection ( connection , localSock , req ) ;
92
+ }
93
+ else
94
+ {
95
+ connection . Close ( ) ;
96
+ localSock . Disconnect ( false ) ;
97
+ }
68
98
}
69
- else
99
+ catch
70
100
{
71
- connection . Close ( ) ;
72
- _localSock . Disconnect ( false ) ;
101
+ localSock . Disconnect ( false ) ;
102
+ throw ;
73
103
}
74
104
}
75
105
76
- private bool Authenticate ( string userId , string token , WsConnection connection , out HorizonRequest req )
106
+ private bool Authenticate ( WsConnection connection , out HorizonRequest req )
77
107
{
78
108
var request = new HorizonRequest
79
109
{
80
- RequestedHost = tunnelDestination ,
81
- RequestedPort = tunnelDestinationPort ,
82
- UserId = userId . ToLower ( ) . Trim ( ) ,
110
+ RequestedHost = _tunnelDestination ,
111
+ RequestedPort = _tunnelDestinationPort ,
112
+ UserId = _userId . ToLower ( ) . Trim ( ) ,
83
113
RequestTime = DateTime . UtcNow ,
84
114
} ;
85
- if ( ProtocolManager . PerformClientHandshake ( connection , token , request ) )
115
+ req = request ;
116
+ if ( ProtocolManager . PerformClientHandshake ( connection , _userToken , request ) )
86
117
{
87
- req = request ;
88
118
return true ;
89
119
}
90
- else
120
+ return false ;
121
+ }
122
+
123
+ private bool AuthenticatePing ( WsConnection connection , out TimeSpan latency )
124
+ {
125
+ var request = new HorizonRequest
126
+ {
127
+ RequestedHost = _tunnelDestination ,
128
+ RequestedPort = _tunnelDestinationPort ,
129
+ UserId = _userId . ToLower ( ) . Trim ( ) ,
130
+ RequestTime = DateTime . UtcNow ,
131
+ PingPacket = true
132
+ } ;
133
+ var response = ProtocolManager . PerformClientPing ( connection , _userToken , request ) ;
134
+
135
+ latency = response - request . RequestTime ;
136
+
137
+ if ( response != DateTime . MinValue )
91
138
{
92
- req = request ;
93
- return false ;
139
+ return true ;
94
140
}
141
+
142
+ return false ;
95
143
}
96
144
}
97
145
}
0 commit comments