private static bool PostAction(Proxy p, Server server, Action action) { var instance = p.Instance; // if we can't issue any commands, bomb out if (instance.AdminUser.IsNullOrEmpty() || instance.AdminPassword.IsNullOrEmpty()) return false; var loginInfo = $"{instance.AdminUser}:{instance.AdminPassword}"; var haproxyUri = new Uri(instance.Url); var requestBody = $"s={server.Name}&action={action.ToString().ToLower()}&b={p.Name}"; var requestHeader = $"POST {haproxyUri.AbsolutePath} HTTP/1.1\r\nHost: {haproxyUri.Host}\r\nContent-Length: {Encoding.GetEncoding("ISO-8859-1").GetBytes(requestBody).Length}\r\nAuthorization: Basic {Convert.ToBase64String(Encoding.Default.GetBytes(loginInfo))}\r\n\r\n"; try { var socket = new System.Net.Sockets.Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); socket.Connect(haproxyUri.Host, haproxyUri.Port); socket.Send(Encoding.UTF8.GetBytes(requestHeader + requestBody)); var responseBytes = new byte[socket.ReceiveBufferSize]; socket.Receive(responseBytes); var response = Encoding.UTF8.GetString(responseBytes); instance.PurgeCache(); return response.StartsWith("HTTP/1.0 303") || response.StartsWith("HTTP/1.1 303"); } catch (Exception e) { Current.LogException(e); return false; } }
public static bool PerformProxyAction(Proxy proxy, Server server, Action action) { if (server != null) { var result = true; Parallel.ForEach(proxy.Servers, _parallelOptions, s => { result = result && PostAction(proxy, s, action); }); return result; } else { return PostAction(proxy, server, action); } }