private void HalfConnect(string sessionId, RegistrationResponse response) { if (response == null) { throw new ArgumentNullException("response"); } response.Success = false; string clientIP = GetClientIP(OperationContext.Current); if (String.IsNullOrEmpty(clientIP)) { response.Description = "Could not determine source IP address."; } else { // Send half-connect to relay if (RelayConnected) { var connectResponse = relay.Proxy.HalfConnect(sessionId, clientIP); if (connectResponse.Result == RelayConnectResponse.ResultCode.Success) { response.Success = true; response.RelayIPAddress = connectResponse.IPAddress; response.RelayPort = connectResponse.Port; } else { response.Description = connectResponse.Description; if (DebugLog != null) { DebugLog.Write(TraceLevel.Warning, "Relay half-connect failed: " + response.Description); } } } else { response.Description = "No relay services are available. Contact support for assistance."; } } }
public RegistrationResponse Register(RegistrationRequest request) { RegistrationResponse response = new RegistrationResponse(); if (String.IsNullOrEmpty(request.ComputerName) || String.IsNullOrEmpty(request.GroupID) || String.IsNullOrEmpty(request.StickID)) { response.Success = false; response.Description = "Incomplete registration request."; return(response); } UserAccount account = null; try { account = data.Accounts.Include(acct => acct.Registrations).FirstOrDefault(acct => acct.GroupID == request.GroupID); } catch (Exception e) { if (DebugLog != null) { while (e.InnerException != null) { DebugLog.Write(TraceLevel.Error, "Database error: " + e.Message); e = e.InnerException; } } response.Success = false; response.Description = "A database error occurred while processing your registration. Please contact technical support."; return(response); } if (account != null) { if (DebugLog != null) { DebugLog.Write(TraceLevel.Info, String.Format("Registering endpoint {0}[{1}] for {2}[{3}] {4} relay", request.ComputerName, request.StickID, account.UserName, account.GroupID, request.RequireRelay ? "force" : "no")); } ServerEndPoint serverEP = account.Registrations.FirstOrDefault(reg => reg.ComputerName == request.ComputerName); if (serverEP != null) { account.Registrations.Remove(serverEP); } string sessionId = Guid.NewGuid().ToString(); if (request.RequireRelay) { HalfConnect(sessionId, response); if (response.Success) { account.Registrations.Add(new ServerEndPoint { StickId = request.StickID, SessionId = sessionId, ComputerName = request.ComputerName, IPAddress = null, Port = 0, RequireRelay = true, RegistrationTime = DateTime.Now }); data.SaveChanges(); } } else if (request.Port <= 0) { response.Success = false; response.Description = "A valid listen port must be specified if relay is not requested."; } else { string clientIP = GetClientIP(OperationContext.Current); if (!String.IsNullOrEmpty(clientIP)) { account.Registrations.Add(new ServerEndPoint { SessionId = sessionId, ComputerName = request.ComputerName, IPAddress = clientIP, Port = request.Port, RequireRelay = false, RegistrationTime = DateTime.Now }); data.SaveChanges(); response.Success = true; } else { response.Description = "Could not determine source IP address."; } } } else { response.Success = false; response.Description = "Group ID not found."; } return(response); }