88using System . Threading . Tasks ;
99using NSmartProxy . Database ;
1010using NSmartProxy . Infrastructure ;
11+ using NSmartProxy . Shared ;
1112
1213namespace NSmartProxy . Authorize
1314{
@@ -37,23 +38,23 @@ public static async Task<SecurityTcpClient> AcceptSecureTcpClientAsync(this TcpL
3738 return stc ;
3839 }
3940
40- public static SecurityTcpClient WrapClient ( this TcpClient client , string secureToken )
41+ public static SecurityTcpClient WrapClient ( this TcpClient client , string secureToken )
4142 {
4243 return new SecurityTcpClient ( secureToken , null , ClientTypeEnum . Client , client ) ;
4344 }
4445
45- public static SecurityTcpClient WrapServer ( this TcpClient client , IDbOperator dbOp )
46+ public static SecurityTcpClient WrapServer ( this TcpClient client , IDbOperator dbOp )
4647 {
4748 return new SecurityTcpClient ( null , dbOp , ClientTypeEnum . Server , client ) ;
4849 }
4950 }
5051
51- public enum AuthState
52+ public enum AuthState : byte
5253 {
53- Success ,
54- Fail ,
55- Timeout ,
56- Error
54+ Success = 0x01 ,
55+ Fail = 0x00 ,
56+ Timeout = 0xFF ,
57+ Error = 0x99 ,
5758 }
5859
5960 public enum ClientTypeEnum
@@ -66,13 +67,14 @@ public enum ClientTypeEnum
6667 /// 2位固定0xF9 2 n
6768 /// 服务端需要指定持久化逻辑,客户端只需token
6869 /// </summary>
69- public class SecurityTcpClient
70+ public class SecurityTcpClient
7071 {
7172 public IDbOperator DbOp ;
7273
7374 public string Token = "" ;
7475 public readonly byte F9 = 0xF9 ; //固定标识位
7576 public String ErrorMessage = "" ;
77+ public bool AllowAnonymousUser = false ;
7678 //TODO 是否校验
7779 public bool IsValid ;
7880 public ClientTypeEnum ClientType ;
@@ -83,30 +85,26 @@ public class SecurityTcpClient
8385 /// </summary>
8486 /// <param name="secureToken"></param>
8587 /// <param name="dbOp"></param>
86- public SecurityTcpClient ( string secureToken , IDbOperator dbOp , ClientTypeEnum clientType , TcpClient client )
88+ public SecurityTcpClient ( string secureToken , IDbOperator dbOp , ClientTypeEnum clientType , TcpClient client )
8789 {
8890 Token = secureToken ;
8991 DbOp = dbOp ;
9092 ClientType = clientType ;
9193 Client = client ;
9294 }
9395
94-
95-
96-
97-
9896
9997 /// <summary>
10098 /// 带加密串传输
10199 /// </summary>
102100 /// <param name="host"></param>
103101 /// <param name="port"></param>
104102 /// <returns></returns>
105- public async Task ConnectWithAuthAsync ( string host , int port )
103+ public async Task < AuthState > ConnectWithAuthAsync ( string host , int port )
106104 {
107105 if ( String . IsNullOrEmpty ( Token ) )
108106 {
109- return ;
107+ return AuthState . Error ;
110108 }
111109
112110 //标识位 token长度 值
@@ -117,10 +115,12 @@ public async Task ConnectWithAuthAsync(string host, int port)
117115 await stream . WriteAsync ( new byte [ ] { F9 } , 0 , 1 ) ; //1标识 长度1
118116 await stream . WriteAsync ( StringUtil . IntTo2Bytes ( Token . Length ) , 0 , 2 ) ; //2token长度 长度2
119117 await stream . WriteAndFlushAsync ( ASCIIEncoding . ASCII . GetBytes ( Token ) ) ; //3token
118+ byte [ ] bytes = new byte [ 1 ] ;
119+ await stream . ReadAsync ( bytes , 0 , 1 ) ;
120+ return ( AuthState ) bytes [ 0 ] ;
120121 }
121122
122123
123-
124124 /// <summary>
125125 /// 服务端校验
126126 /// </summary>
@@ -159,13 +159,20 @@ public async Task<AuthResult> AuthorizeAsync()
159159
160160 var token = ASCIIEncoding . ASCII . GetString ( tokenBytes ) ;
161161 //TODO ***校验Token
162- if ( token == "notoken" )
162+ if ( token == Global . NO_TOKEN_STRING )
163163 {
164- return new AuthResult ( )
165- {
166- ErrorMessage = "校验成功" ,
167- ResultState = AuthState . Success
168- } ;
164+ if ( AllowAnonymousUser )
165+ return new AuthResult ( )
166+ {
167+ ErrorMessage = "校验成功" ,
168+ ResultState = AuthState . Success
169+ } ;
170+ else
171+ return new AuthResult ( )
172+ {
173+ ErrorMessage = "校验失败,服务端不支持匿名登陆" ,
174+ ResultState = AuthState . Fail
175+ } ;
169176 }
170177 else
171178 {
0 commit comments