using System; using System.Collections.Generic; using System.Text.RegularExpressions; namespace Netch.Models.Server.ShadowsocksR { public class ShadowsocksR : Server { public ShadowsocksR() { this.Type = ServerType.ShadowsocksR; } /// /// 密码 /// [Newtonsoft.Json.JsonProperty("passwd")] public string Passwd; /// /// 加密 /// [Newtonsoft.Json.JsonProperty("method")] public string Method; /// /// 协议 /// [Newtonsoft.Json.JsonProperty("prot")] public string Prot; /// /// 协议参数 /// [Newtonsoft.Json.JsonProperty("protparam")] public string ProtParam; /// /// 混淆 /// [Newtonsoft.Json.JsonProperty("obfs")] public string OBFS; /// /// 混淆参数 /// [Newtonsoft.Json.JsonProperty("obfsparam")] public string OBFSParam; /// /// 解析链接 /// /// 链接 /// 是否成功 public bool ParseLink(string link) { try { var ssr = new Regex(@"ssr://([A-Za-z0-9+/=_-]+)", RegexOptions.IgnoreCase).Match(link); if (!ssr.Success) { return false; } var data = Utils.Base64.Decode.URLSafe(ssr.Groups[1].Value); var dict = new Dictionary(); var offset = data.IndexOf(@"?", StringComparison.Ordinal); if (offset > 0) { dict = ParseParam(data.Substring(offset + 1)); data = data.Substring(0, offset); } if (data.IndexOf("/", StringComparison.Ordinal) >= 0) { data = data.Substring(0, data.LastIndexOf("/", StringComparison.Ordinal)); } var matches = new Regex(@"^(.+):([^:]+):([^:]*):([^:]+):([^:]*):([^:]+)").Match(data); if (!matches.Success) { return false; } if (dict.ContainsKey("remarks")) { this.Remark = Utils.Base64.Decode.URLSafe(dict["remarks"]); } this.Host = matches.Groups[1].Value; if (!ushort.TryParse(matches.Groups[2].Value, out this.Port)) { return false; } this.Passwd = Utils.Base64.Decode.URLSafe(matches.Groups[6].Value); this.Method = matches.Groups[4].Value.ToLower(); this.Prot = (matches.Groups[3].Value.Length == 0 ? "origin" : matches.Groups[3].Value).Replace("_compatible", String.Empty).ToLower(); if (dict.ContainsKey("protoparam")) { this.ProtParam = Utils.Base64.Decode.URLSafe(dict["protoparam"]); } this.OBFS = (matches.Groups[5].Value.Length == 0 ? @"plain" : matches.Groups[5].Value).Replace("_compatible", String.Empty).ToLower(); if (dict.ContainsKey("obfsparam")) { this.OBFSParam = Utils.Base64.Decode.URLSafe(dict["obfsparam"]); } } catch (Exception e) { global::Netch.Global.Logger.Warning(e.ToString()); return false; } return true; } private static Dictionary ParseParam(string str) { var dict = new Dictionary(); var obfs = str.Split('&'); for (int i = 0; i < str.Length; i++) { if (obfs[i].IndexOf('=') > 0) { var index = obfs[i].IndexOf('='); var k = obfs[i].Substring(0, index); var v = obfs[i].Substring(index + 1); dict[k] = v; } } return dict; } } }