├── .gitignore ├── L2Proxy.sln ├── L2Proxy ├── Auth │ └── ApiKeyAttribute.cs ├── Controllers │ ├── BlacklistController.cs │ └── ProxyController.cs ├── L2Proxy.csproj ├── Program.cs ├── Properties │ └── launchSettings.json ├── Proxy │ ├── BlacklistService.cs │ ├── L2ClientConnection.cs │ ├── L2Proxy.cs │ └── ProxyService.cs ├── Settings │ └── ProxySettings.cs ├── Startup.cs └── appsettings.json ├── LICENSE └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | # User-specific files 5 | *.suo 6 | *.user 7 | *.userosscache 8 | *.sln.docstates 9 | 10 | # User-specific files (MonoDevelop/Xamarin Studio) 11 | *.userprefs 12 | 13 | # Build results 14 | [Dd]ebug/ 15 | [Dd]ebugPublic/ 16 | [Rr]elease/ 17 | [Rr]eleases/ 18 | x64/ 19 | x86/ 20 | bld/ 21 | [Bb]in/ 22 | [Oo]bj/ 23 | [Ll]og/ 24 | 25 | # Visual Studio 2015 cache/options directory 26 | .vs/ 27 | # Uncomment if you have tasks that create the project's static files in wwwroot 28 | #wwwroot/ 29 | 30 | # MSTest test Results 31 | [Tt]est[Rr]esult*/ 32 | [Bb]uild[Ll]og.* 33 | 34 | # NUNIT 35 | *.VisualState.xml 36 | TestResult.xml 37 | 38 | # Build Results of an ATL Project 39 | [Dd]ebugPS/ 40 | [Rr]eleasePS/ 41 | dlldata.c 42 | 43 | # DNX 44 | project.lock.json 45 | project.fragment.lock.json 46 | artifacts/ 47 | 48 | *_i.c 49 | *_p.c 50 | *_i.h 51 | *.ilk 52 | *.meta 53 | *.obj 54 | *.pch 55 | *.pdb 56 | *.pgc 57 | *.pgd 58 | *.rsp 59 | *.sbr 60 | *.tlb 61 | *.tli 62 | *.tlh 63 | *.tmp 64 | *.tmp_proj 65 | *.log 66 | *.vspscc 67 | *.vssscc 68 | .builds 69 | *.pidb 70 | *.svclog 71 | *.scc 72 | 73 | # Chutzpah Test files 74 | _Chutzpah* 75 | 76 | # Visual C++ cache files 77 | ipch/ 78 | *.aps 79 | *.ncb 80 | *.opendb 81 | *.opensdf 82 | *.sdf 83 | *.cachefile 84 | *.VC.db 85 | *.VC.VC.opendb 86 | 87 | # Visual Studio profiler 88 | *.psess 89 | *.vsp 90 | *.vspx 91 | *.sap 92 | 93 | # TFS 2012 Local Workspace 94 | $tf/ 95 | 96 | # Guidance Automation Toolkit 97 | *.gpState 98 | 99 | # ReSharper is a .NET coding add-in 100 | _ReSharper*/ 101 | *.[Rr]e[Ss]harper 102 | *.DotSettings.user 103 | 104 | # JustCode is a .NET coding add-in 105 | .JustCode 106 | 107 | # TeamCity is a build add-in 108 | _TeamCity* 109 | 110 | # DotCover is a Code Coverage Tool 111 | *.dotCover 112 | 113 | # NCrunch 114 | _NCrunch_* 115 | .*crunch*.local.xml 116 | nCrunchTemp_* 117 | 118 | # MightyMoose 119 | *.mm.* 120 | AutoTest.Net/ 121 | 122 | # Web workbench (sass) 123 | .sass-cache/ 124 | 125 | # Installshield output folder 126 | [Ee]xpress/ 127 | 128 | # DocProject is a documentation generator add-in 129 | DocProject/buildhelp/ 130 | DocProject/Help/*.HxT 131 | DocProject/Help/*.HxC 132 | DocProject/Help/*.hhc 133 | DocProject/Help/*.hhk 134 | DocProject/Help/*.hhp 135 | DocProject/Help/Html2 136 | DocProject/Help/html 137 | 138 | # Click-Once directory 139 | publish/ 140 | 141 | # Publish Web Output 142 | *.[Pp]ublish.xml 143 | *.azurePubxml 144 | # TODO: Comment the next line if you want to checkin your web deploy settings 145 | # but database connection strings (with potential passwords) will be unencrypted 146 | #*.pubxml 147 | *.publishproj 148 | 149 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 150 | # checkin your Azure Web App publish settings, but sensitive information contained 151 | # in these scripts will be unencrypted 152 | PublishScripts/ 153 | 154 | # NuGet Packages 155 | *.nupkg 156 | # The packages folder can be ignored because of Package Restore 157 | **/packages/* 158 | # except build/, which is used as an MSBuild target. 159 | !**/packages/build/ 160 | # Uncomment if necessary however generally it will be regenerated when needed 161 | #!**/packages/repositories.config 162 | # NuGet v3's project.json files produces more ignoreable files 163 | *.nuget.props 164 | *.nuget.targets 165 | 166 | # Microsoft Azure Build Output 167 | csx/ 168 | *.build.csdef 169 | 170 | # Microsoft Azure Emulator 171 | ecf/ 172 | rcf/ 173 | 174 | # Windows Store app package directories and files 175 | AppPackages/ 176 | BundleArtifacts/ 177 | Package.StoreAssociation.xml 178 | _pkginfo.txt 179 | 180 | # Visual Studio cache files 181 | # files ending in .cache can be ignored 182 | *.[Cc]ache 183 | # but keep track of directories ending in .cache 184 | !*.[Cc]ache/ 185 | 186 | # Others 187 | ClientBin/ 188 | ~$* 189 | *~ 190 | *.dbmdl 191 | *.dbproj.schemaview 192 | *.jfm 193 | *.pfx 194 | *.publishsettings 195 | node_modules/ 196 | orleans.codegen.cs 197 | 198 | # Since there are multiple workflows, uncomment next line to ignore bower_components 199 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 200 | #bower_components/ 201 | 202 | # RIA/Silverlight projects 203 | Generated_Code/ 204 | 205 | # Backup & report files from converting an old project file 206 | # to a newer Visual Studio version. Backup files are not needed, 207 | # because we have git ;-) 208 | _UpgradeReport_Files/ 209 | Backup*/ 210 | UpgradeLog*.XML 211 | UpgradeLog*.htm 212 | 213 | # SQL Server files 214 | *.mdf 215 | *.ldf 216 | 217 | # Business Intelligence projects 218 | *.rdl.data 219 | *.bim.layout 220 | *.bim_*.settings 221 | 222 | # Microsoft Fakes 223 | FakesAssemblies/ 224 | 225 | # GhostDoc plugin setting file 226 | *.GhostDoc.xml 227 | 228 | # Node.js Tools for Visual Studio 229 | .ntvs_analysis.dat 230 | 231 | # Visual Studio 6 build log 232 | *.plg 233 | 234 | # Visual Studio 6 workspace options file 235 | *.opt 236 | 237 | # Visual Studio LightSwitch build output 238 | **/*.HTMLClient/GeneratedArtifacts 239 | **/*.DesktopClient/GeneratedArtifacts 240 | **/*.DesktopClient/ModelManifest.xml 241 | **/*.Server/GeneratedArtifacts 242 | **/*.Server/ModelManifest.xml 243 | _Pvt_Extensions 244 | 245 | # Paket dependency manager 246 | .paket/paket.exe 247 | paket-files/ 248 | 249 | # FAKE - F# Make 250 | .fake/ 251 | 252 | # JetBrains Rider 253 | .idea/ 254 | *.sln.iml 255 | 256 | # CodeRush 257 | .cr/ 258 | 259 | # Python Tools for Visual Studio (PTVS) 260 | __pycache__/ 261 | *.pyc 262 | /obj/ 263 | -------------------------------------------------------------------------------- /L2Proxy.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "L2Proxy", "L2Proxy\L2Proxy.csproj", "{586FE7F0-C3CE-42FA-B38A-88E5DB03B773}" 4 | EndProject 5 | Global 6 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 7 | Debug|Any CPU = Debug|Any CPU 8 | Release|Any CPU = Release|Any CPU 9 | EndGlobalSection 10 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 11 | {586FE7F0-C3CE-42FA-B38A-88E5DB03B773}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 12 | {586FE7F0-C3CE-42FA-B38A-88E5DB03B773}.Debug|Any CPU.Build.0 = Debug|Any CPU 13 | {586FE7F0-C3CE-42FA-B38A-88E5DB03B773}.Release|Any CPU.ActiveCfg = Release|Any CPU 14 | {586FE7F0-C3CE-42FA-B38A-88E5DB03B773}.Release|Any CPU.Build.0 = Release|Any CPU 15 | EndGlobalSection 16 | EndGlobal 17 | -------------------------------------------------------------------------------- /L2Proxy/Auth/ApiKeyAttribute.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading.Tasks; 3 | using Microsoft.AspNetCore.Mvc; 4 | using Microsoft.AspNetCore.Mvc.Filters; 5 | using Microsoft.Extensions.Configuration; 6 | using Microsoft.Extensions.DependencyInjection; 7 | 8 | namespace L2Proxy.Auth 9 | { 10 | [AttributeUsage(AttributeTargets.Class)] 11 | public class ApiKeyAttribute : Attribute, IAsyncActionFilter 12 | { 13 | private const string APIKEYNAME = "ApiKey"; 14 | public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) 15 | { 16 | if (!context.HttpContext.Request.Headers.TryGetValue("x-api-key", out var extractedApiKey)) 17 | { 18 | context.Result = new ContentResult() 19 | { 20 | StatusCode = 401, 21 | Content = "Invalid API key" 22 | }; 23 | return; 24 | } 25 | 26 | var appSettings = context.HttpContext.RequestServices.GetRequiredService(); 27 | 28 | var apiKey = appSettings.GetValue("ApiSettings:ApiKey"); 29 | 30 | if (!apiKey.Equals(extractedApiKey)) 31 | { 32 | context.Result = new ContentResult() 33 | { 34 | StatusCode = 401, 35 | Content = "Invalid API key" 36 | }; 37 | return; 38 | } 39 | 40 | await next(); 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /L2Proxy/Controllers/BlacklistController.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Threading.Tasks; 3 | using L2Proxy.Auth; 4 | using L2Proxy.Proxy; 5 | using Microsoft.AspNetCore.Mvc; 6 | 7 | namespace L2Proxy.Controllers 8 | { 9 | [ApiController] 10 | [ApiKey] 11 | [Route("api/blacklist")] 12 | public class BlacklistController : ControllerBase 13 | { 14 | private readonly IBlacklistService _blacklistService; 15 | private readonly IEnumerable _proxies; 16 | 17 | public BlacklistController(IBlacklistService blacklistService, IEnumerable proxies) 18 | { 19 | _blacklistService = blacklistService; 20 | _proxies = proxies; 21 | } 22 | 23 | [HttpGet("")] 24 | public IActionResult GetAll() 25 | { 26 | var blacklistedIps = _blacklistService.GetBlacklistedIps(); 27 | return Ok(blacklistedIps); 28 | } 29 | 30 | [HttpGet("{ip}")] 31 | public IActionResult IsBlacklisted([FromRoute]string ip) 32 | { 33 | var isBlacklisted = _blacklistService.IsBlacklisted(ip); 34 | return Ok(isBlacklisted); 35 | } 36 | 37 | [HttpPost("{ip}")] 38 | public async Task BlacklistIp(string ip) 39 | { 40 | await _blacklistService.BlacklistIp(ip); 41 | foreach (var l2Proxy in _proxies) 42 | { 43 | foreach (var (key, value) in l2Proxy.ActiveConnections) 44 | { 45 | if (key.ToLowerInvariant().StartsWith(ip.ToLowerInvariant())) 46 | { 47 | value.Disconnect(); 48 | } 49 | } 50 | } 51 | return Ok(); 52 | } 53 | 54 | [HttpDelete("{ip}")] 55 | public async Task RemoveBlacklistedIp(string ip) 56 | { 57 | await _blacklistService.RemoveBlacklistedIp(ip); 58 | return Ok(); 59 | } 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /L2Proxy/Controllers/ProxyController.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Linq; 3 | using L2Proxy.Auth; 4 | using Microsoft.AspNetCore.Mvc; 5 | 6 | namespace L2Proxy.Controllers 7 | { 8 | [ApiController] 9 | [Route("api/proxies")] 10 | [ApiKey] 11 | public class ProxyController : ControllerBase 12 | { 13 | private readonly IEnumerable _proxies; 14 | 15 | public ProxyController(IEnumerable proxies) 16 | { 17 | _proxies = proxies; 18 | } 19 | 20 | [HttpGet("")] 21 | public IActionResult GetProxies() 22 | { 23 | return Ok(new 24 | { 25 | Proxies = _proxies.Select(x=> new 26 | { 27 | x.ProxyInfo.ProxyHost, 28 | x.ProxyInfo.ProxyPort, 29 | x.ProxyInfo.L2ServerHost, 30 | x.ProxyInfo.L2ServerPort, 31 | ActiveConnectionCount = _proxies 32 | .Single(xx => xx.ProxyInfo.ProxyHost == x.ProxyInfo.ProxyHost && xx.ProxyInfo.ProxyPort == x.ProxyInfo.ProxyPort) 33 | .ActiveConnections.Count, 34 | x.ProxyInfo.MaxConnections 35 | }).ToList() 36 | }); 37 | } 38 | 39 | [HttpGet("{proxyHost}/{proxyPort}")] 40 | public IActionResult GetActiveConnections(string proxyHost, ushort proxyPort) 41 | { 42 | var proxy = _proxies.SingleOrDefault(x => 43 | x.ProxyInfo.ProxyHost == proxyHost && x.ProxyInfo.ProxyPort == proxyPort); 44 | 45 | if (proxy is null) 46 | { 47 | return NotFound(); 48 | } 49 | 50 | var activeConnections = proxy.ActiveConnections.Values; 51 | 52 | return Ok(new 53 | { 54 | ActiveConnectionCount = activeConnections.Count, 55 | ActiveConnections = activeConnections.Select(x => x.ClientEndpoint.ToString()).ToList(), 56 | proxy.ProxyInfo.MaxConnections 57 | }); 58 | } 59 | 60 | [HttpDelete("{proxyHost}/{proxyPort}/{clientHost}/{clientPort}")] 61 | public IActionResult DisconnectClient(string proxyHost, ushort proxyPort, string clientHost, ushort clientPort) 62 | { 63 | var proxy = _proxies.SingleOrDefault(x => 64 | x.ProxyInfo.ProxyHost == proxyHost && x.ProxyInfo.ProxyPort == proxyPort); 65 | 66 | if (proxy is null) 67 | { 68 | return NotFound(); 69 | } 70 | 71 | var connectionKey = $"{clientHost}:{clientPort}|{proxy.ProxyInfo.L2ServerHost}:{proxy.ProxyInfo.L2ServerPort}"; 72 | proxy.ActiveConnections.TryGetValue(connectionKey, out var connection); 73 | connection?.Disconnect(); 74 | return NoContent(); 75 | } 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /L2Proxy/L2Proxy.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | net5.0 4 | 5 | 6 | -------------------------------------------------------------------------------- /L2Proxy/Program.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.AspNetCore.Hosting; 2 | using Microsoft.Extensions.Configuration; 3 | using Microsoft.Extensions.Hosting; 4 | 5 | namespace L2Proxy 6 | { 7 | public class Program 8 | { 9 | public static void Main(string[] args) 10 | { 11 | CreateHostBuilder(args).Build().Run(); 12 | } 13 | 14 | public static IHostBuilder CreateHostBuilder(string[] args) 15 | { 16 | var configuration = new ConfigurationBuilder() 17 | .AddEnvironmentVariables() 18 | .AddCommandLine(args) 19 | .AddJsonFile("appsettings.json") 20 | .Build(); 21 | 22 | return Host.CreateDefaultBuilder(args) 23 | .ConfigureWebHostDefaults(webBuilder => 24 | { 25 | webBuilder 26 | .UseConfiguration(configuration) 27 | .UseStartup() 28 | .UseUrls(configuration.GetValue("ApiSettings:Url")); 29 | }); 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /L2Proxy/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json.schemastore.org/launchsettings.json", 3 | "profiles": { 4 | "L2Proxy": { 5 | "commandName": "Project", 6 | "dotnetRunMessages": "true", 7 | "launchBrowser": false, 8 | "environmentVariables": { 9 | "ASPNETCORE_ENVIRONMENT": "Development" 10 | } 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /L2Proxy/Proxy/BlacklistService.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.IO; 3 | using System.Linq; 4 | using System.Net; 5 | using System.Threading.Tasks; 6 | using Microsoft.Extensions.Logging; 7 | 8 | namespace L2Proxy.Proxy 9 | { 10 | public class BlacklistService : IBlacklistService 11 | { 12 | private const string BlacklistFilePath = "./blacklist.txt"; 13 | private readonly List _blacklistedIps = new(); 14 | private readonly ILogger _logger; 15 | 16 | public BlacklistService(ILogger logger) 17 | { 18 | _logger = logger; 19 | if (!File.Exists(BlacklistFilePath)) 20 | { 21 | using (File.Create(BlacklistFilePath)){} 22 | logger.LogInformation("Created a new blacklist.txt file"); 23 | } 24 | 25 | foreach (var ipLine in File.ReadAllLines(BlacklistFilePath)) 26 | { 27 | _blacklistedIps.Add(ipLine.ToLowerInvariant()); 28 | } 29 | 30 | _blacklistedIps = _blacklistedIps.Distinct().ToList(); 31 | logger.LogInformation($"Loaded {_blacklistedIps.Count} blacklisted ips"); 32 | } 33 | 34 | public async Task BlacklistIp(string ip) 35 | { 36 | if (!IsBlacklisted(ip)) 37 | { 38 | _blacklistedIps.Add(ip.ToLowerInvariant()); 39 | await File.WriteAllLinesAsync(BlacklistFilePath, _blacklistedIps); 40 | _logger.LogInformation($"Blacklisted IP {ip}"); 41 | } 42 | } 43 | 44 | public IEnumerable GetBlacklistedIps() 45 | { 46 | return _blacklistedIps; 47 | } 48 | 49 | public bool IsBlacklisted(EndPoint endpoint) 50 | { 51 | if (endpoint is null) 52 | { 53 | return false; 54 | } 55 | 56 | var ip = StripPortFromEndPoint(endpoint.ToString()); 57 | 58 | if (string.IsNullOrWhiteSpace(ip) || string.IsNullOrEmpty(ip)) 59 | { 60 | return false; 61 | } 62 | 63 | return _blacklistedIps.Contains(ip.ToLowerInvariant()); 64 | } 65 | 66 | public bool IsBlacklisted(string ip) 67 | { 68 | if (string.IsNullOrEmpty(ip)) 69 | { 70 | return false; 71 | } 72 | 73 | var ipWithoutPort = StripPortFromEndPoint(ip); 74 | 75 | if (string.IsNullOrWhiteSpace(ipWithoutPort) || string.IsNullOrEmpty(ipWithoutPort)) 76 | { 77 | return false; 78 | } 79 | 80 | return _blacklistedIps.Contains(ipWithoutPort.ToLowerInvariant()); 81 | } 82 | 83 | public async Task RemoveBlacklistedIp(string ip) 84 | { 85 | if (!IsBlacklisted(ip.ToLowerInvariant())) 86 | { 87 | return; 88 | } 89 | 90 | _blacklistedIps.Remove(ip.ToLowerInvariant()); 91 | await File.WriteAllLinesAsync(BlacklistFilePath, _blacklistedIps); 92 | _logger.LogInformation($"Removed IP {ip} from the blacklist"); 93 | } 94 | 95 | private string StripPortFromEndPoint(string endPoint) 96 | { 97 | var splitList = endPoint.Split(':'); 98 | endPoint = splitList.Length switch 99 | { 100 | > 2 => IPAddress.Parse(endPoint).ToString(), 101 | 2 => splitList[0], 102 | _ => endPoint 103 | }; 104 | 105 | return endPoint; 106 | } 107 | } 108 | 109 | public interface IBlacklistService 110 | { 111 | Task BlacklistIp(string ip); 112 | 113 | IEnumerable GetBlacklistedIps(); 114 | 115 | bool IsBlacklisted(EndPoint ip); 116 | 117 | bool IsBlacklisted(string ip); 118 | 119 | Task RemoveBlacklistedIp(string ip); 120 | } 121 | } 122 | -------------------------------------------------------------------------------- /L2Proxy/Proxy/L2ClientConnection.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Net; 3 | using System.Net.Sockets; 4 | using System.Threading.Tasks; 5 | using Microsoft.Extensions.Logging; 6 | 7 | namespace L2Proxy.Proxy 8 | { 9 | public class L2ClientConnection 10 | { 11 | private readonly ILogger _logger; 12 | private readonly TcpClient _remoteClient; 13 | private readonly IPEndPoint _remoteServerIpEndPoint; 14 | private readonly Action _onDisconnect; 15 | private readonly TcpClient _client; 16 | 17 | public L2ClientConnection(ILogger logger, TcpClient remoteClient, IPEndPoint remoteServerIpEndPoint, 18 | Action onDisconnect) 19 | { 20 | _client = new TcpClient(); 21 | _logger = logger; 22 | _remoteClient = remoteClient; 23 | _remoteServerIpEndPoint = remoteServerIpEndPoint; 24 | _onDisconnect = onDisconnect; 25 | _client.NoDelay = true; 26 | ClientEndpoint = (IPEndPoint) _remoteClient.Client.RemoteEndPoint; 27 | } 28 | 29 | public IPEndPoint ClientEndpoint { get; } 30 | 31 | public async Task Handle() 32 | { 33 | try 34 | { 35 | using (_remoteClient) 36 | using (_client) 37 | { 38 | await _client.ConnectAsync(_remoteServerIpEndPoint.Address, _remoteServerIpEndPoint.Port); 39 | var serverStream = _client.GetStream(); 40 | var remoteStream = _remoteClient.GetStream(); 41 | await Task.WhenAny(remoteStream.CopyToAsync(serverStream), serverStream.CopyToAsync(remoteStream)); 42 | } 43 | } 44 | catch{} 45 | finally 46 | { 47 | _logger.LogInformation($"Closed {ClientEndpoint} => {_remoteServerIpEndPoint}"); 48 | _onDisconnect.Invoke(); 49 | Disconnect(); 50 | } 51 | } 52 | 53 | public override string ToString() 54 | { 55 | return ClientEndpoint.ToString(); 56 | } 57 | 58 | public void Disconnect() 59 | { 60 | _client?.GetStream().Close(); 61 | _client?.Close(); 62 | _client?.Dispose(); 63 | _remoteClient?.GetStream().Close(); 64 | _remoteClient?.Close(); 65 | _remoteClient?.Dispose(); 66 | } 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /L2Proxy/Proxy/L2Proxy.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Concurrent; 3 | using System.Linq; 4 | using System.Net; 5 | using System.Net.Sockets; 6 | using System.Threading; 7 | using System.Threading.Tasks; 8 | using L2Proxy.Settings; 9 | using Microsoft.Extensions.Logging; 10 | 11 | namespace L2Proxy.Proxy 12 | { 13 | public class L2Proxy 14 | { 15 | private readonly ILogger _logger; 16 | private readonly IBlacklistService _blacklistService; 17 | 18 | public L2Proxy(ILogger logger, ProxyInfo proxyInfo, IBlacklistService blacklistService) 19 | { 20 | _logger = logger; 21 | ProxyInfo = proxyInfo; 22 | _blacklistService = blacklistService; 23 | } 24 | 25 | public ConcurrentDictionary ActiveConnections { get; } = new(); 26 | 27 | public ProxyInfo ProxyInfo { get; } 28 | 29 | public async Task StartAsync(CancellationToken cancellationToken) 30 | { 31 | var localIpAddress = string.IsNullOrEmpty(ProxyInfo.ProxyHost) ? IPAddress.IPv6Any : IPAddress.Parse(ProxyInfo.ProxyHost); 32 | var server = new TcpListener(new IPEndPoint(localIpAddress, ProxyInfo.ProxyPort)); 33 | server.Server.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true); 34 | server.Start(); 35 | 36 | _logger.LogInformation($"L2Proxy started {ProxyInfo.ProxyPort} -> {ProxyInfo.L2ServerHost}:{ProxyInfo.L2ServerPort}"); 37 | while (!cancellationToken.IsCancellationRequested) 38 | { 39 | try 40 | { 41 | var remoteClient = await server.AcceptTcpClientAsync(); 42 | remoteClient.NoDelay = true; 43 | 44 | if (_blacklistService.IsBlacklisted(remoteClient.Client.RemoteEndPoint)) 45 | { 46 | DisconnectClient(remoteClient); 47 | continue; 48 | } 49 | 50 | if (ProxyInfo.MaxConnections > 0 && ActiveConnections.Count >= ProxyInfo.MaxConnections) 51 | { 52 | DisconnectClient(remoteClient); 53 | continue; 54 | } 55 | 56 | var ips = await Dns.GetHostAddressesAsync(ProxyInfo.L2ServerHost); 57 | var gameserverIpEndpoint = new IPEndPoint(ips.First(), ProxyInfo.L2ServerPort); 58 | var connectionKey = $"{(IPEndPoint) remoteClient.Client.RemoteEndPoint}|{gameserverIpEndpoint}"; 59 | var client = new L2ClientConnection(_logger, remoteClient, gameserverIpEndpoint, () => 60 | { 61 | ActiveConnections.TryRemove(connectionKey, out var removedConnection); 62 | }); 63 | _logger.LogInformation($"Established {(IPEndPoint)remoteClient.Client.RemoteEndPoint} => {gameserverIpEndpoint}"); 64 | 65 | ActiveConnections.TryAdd(connectionKey, client); 66 | 67 | #pragma warning disable 4014 68 | client.Handle(); 69 | #pragma warning restore 4014 70 | } 71 | catch (Exception ex) 72 | { 73 | _logger.LogError(ex, "Failed to handle connection."); 74 | } 75 | } 76 | } 77 | 78 | private static void DisconnectClient(TcpClient remoteClient) 79 | { 80 | remoteClient.GetStream()?.Close(); 81 | remoteClient?.Close(); 82 | remoteClient?.Dispose(); 83 | } 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /L2Proxy/Proxy/ProxyService.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Linq; 3 | using System.Threading; 4 | using System.Threading.Tasks; 5 | using Microsoft.Extensions.Hosting; 6 | 7 | namespace L2Proxy.Proxy 8 | { 9 | public class ProxyService : BackgroundService 10 | { 11 | private readonly IEnumerable _proxies; 12 | 13 | public ProxyService(IEnumerable proxies) 14 | { 15 | _proxies = proxies; 16 | } 17 | 18 | protected override Task ExecuteAsync(CancellationToken stoppingToken) 19 | { 20 | var proxyTasks = _proxies.Select(x => x.StartAsync(stoppingToken)); 21 | return Task.WhenAll(proxyTasks); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /L2Proxy/Settings/ProxySettings.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace L2Proxy.Settings 4 | { 5 | public class ProxySettings 6 | { 7 | public ICollection Proxies { get; set; } 8 | } 9 | 10 | public class ProxyInfo 11 | { 12 | public string ProxyHost { get; init; } 13 | 14 | public ushort ProxyPort { get; init; } 15 | 16 | public string L2ServerHost { get; init; } 17 | 18 | public ushort L2ServerPort { get; init; } 19 | 20 | public int MaxConnections { get; init; } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /L2Proxy/Startup.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Linq; 3 | using System.Threading.Tasks; 4 | using L2Proxy.Proxy; 5 | using L2Proxy.Settings; 6 | using Microsoft.AspNetCore.Builder; 7 | using Microsoft.AspNetCore.Hosting; 8 | using Microsoft.AspNetCore.Http; 9 | using Microsoft.Extensions.Configuration; 10 | using Microsoft.Extensions.DependencyInjection; 11 | using Microsoft.Extensions.Logging; 12 | using Microsoft.Extensions.Options; 13 | 14 | namespace L2Proxy 15 | { 16 | public class Startup 17 | { 18 | public Startup(IConfiguration configuration) 19 | { 20 | Configuration = configuration; 21 | } 22 | 23 | public IConfiguration Configuration { get; } 24 | 25 | public void ConfigureServices(IServiceCollection services) 26 | { 27 | services.AddControllers(); 28 | services.AddHostedService(); 29 | services.AddSingleton(); 30 | services.AddSingleton>(x => 31 | { 32 | var proxySettings = x.GetRequiredService>().Value; 33 | var logger = x.GetRequiredService>(); 34 | var blacklistService = x.GetRequiredService(); 35 | return proxySettings.Proxies.Select(xx => new Proxy.L2Proxy(logger, xx, blacklistService)).ToList(); 36 | }); 37 | services.Configure(Configuration.GetSection(nameof(ProxySettings))); 38 | } 39 | 40 | public void Configure(IApplicationBuilder app, IWebHostEnvironment env) 41 | { 42 | app.UseRouting(); 43 | app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); 44 | ConfigurePingEndpoint(app); 45 | } 46 | 47 | private static void ConfigurePingEndpoint(IApplicationBuilder app) 48 | { 49 | app.Map("/_ping", builder => builder.Run(async context => 50 | { 51 | context.Response.StatusCode = 200; 52 | await context.Response.WriteAsync("pong"); 53 | })); 54 | } 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /L2Proxy/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "ProxySettings": { 3 | "Proxies": [ 4 | { 5 | "ProxyHost": "127.0.0.1", 6 | "ProxyPort": 7778, 7 | "L2ServerHost": "127.0.0.1", 8 | "L2ServerPort": 7777, 9 | "MaxConnections": -1 10 | }, 11 | { 12 | "ProxyHost": "127.0.0.1", 13 | "ProxyPort": 7779, 14 | "L2ServerHost": "127.0.0.1", 15 | "L2ServerPort": 7777, 16 | "MaxConnections": -1 17 | } 18 | ] 19 | }, 20 | "ApiSettings": { 21 | "Url": "http://0.0.0.0:6969", 22 | "ApiKey": "changeit" 23 | }, 24 | "Logging": { 25 | "LogLevel": { 26 | "Default": "Information", 27 | "Microsoft": "Warning", 28 | "Microsoft.Hosting.Lifetime": "Information" 29 | } 30 | }, 31 | "AllowedHosts": "*" 32 | } 33 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Nick Chapsas 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # L2Proxy 2 | A simple MITM Proxy for Lineage 2 3 | 4 | Java side link: https://gist.github.com/Elfocrash/644d99e27cfb798220340aadffd684e6 5 | 6 | ### Features 7 | 8 | * Gameserver invisibility option - You are able to hide your server behind the proxies and keep your real server IP secret 9 | * RealIP - Usually with MITM proxies, since the traffic is funnelled from one server, you normally lose the real IP of the player which limits a lot of the functionality that you might have implemented. L2Proxy allows the LoginServer to pass the real IP of the used to the Gameserver during the Login->Gameserver player handoff 10 | * An API - You can use the API in L2Proxy to check the stats of your Proxies, see the active connections to it and even disconnect a specific use IP or blacklist it 11 | * IP Blacklist - You can blacklist a specific IP its connection will be rejected on the proxy level before it ever gets to the gameserver. This includes malicious connections. You can also use the API to blacklist someone and get them instantly disconnected.  12 | * Multiple Proxies from one app - No real reason for this to exist but I added it anyway 13 | 14 | ### API Actions 15 | 16 | * Get all proxies - `GET` `http://localhost:6969/api/proxies` 17 | * Get proxy by IP and Port - `GET` `http://localhost:6969/api/proxies/127.0.0.1/7778` (IP is the proxy ip and 7778 is the proxy port) 18 | * Disconnect an active connection - `DELETE` `http://localhost:6969/api/proxies/127.0.0.1/7778/127.0.0.1/11571` (First IP is the proxy IP, second ip is the client ip, first port is the proxy port and second port is a client port) 19 | * Get all blacklisted IPs - `GET` `http://localhost:6969/api/blacklist` 20 | * Check if IP is blacklisted - `GET` `http://localhost:6969/api/blacklist/127.0.0.1` (The IP is the user IP) 21 | * Blacklist an IP and disconnect used - `POST` `http://localhost:6969/api/blacklist/127.0.0.1` (The IP is the user IP) 22 | * Removed an IP from the blacklist - `DELETE` `http://localhost:6969/api/blacklist/127.0.0.1` (The IP is the user IP) 23 | --------------------------------------------------------------------------------