private static void ImportUsers()
 {
     if (allConf["user-conf"].Count >= 1)
     {
         string pathOfUsersConf = allConf["user-conf"][0];
         if (File.Exists(pathOfUsersConf))
         {
             string usersText = File.ReadAllText(pathOfUsersConf);
             usersText = usersText.Replace("\r\n", "\n");
             string[] usersArray = usersText.Split('\n');
             usersArray = Format(usersArray);
             foreach (string line in usersArray)
             {
                 if (EagleTunnelUser.TryParse(line, out EagleTunnelUser user))
                 {
                     if (!Users.ContainsKey(user.ID))
                     {
                         Users.Add(user.ID, user);
                     }
                 }
             }
         }
         else
         {
             Console.WriteLine("user-conf file not found: {0}", pathOfUsersConf);
         }
     }
 }
        private static EagleTunnelUser CheckAuthen(Tunnel tunnel)
        {
            EagleTunnelUser result = null;

            if (Conf.allConf.ContainsKey("user-check") && Conf.allConf["user-check"][0] == "on")
            {
                string req = tunnel.ReadStringL();
                if (!string.IsNullOrEmpty(req))
                {
                    if (EagleTunnelUser.TryParse(req, out EagleTunnelUser user, false))
                    {
                        result = EagleTunnelUser.Check(user.ID, user.Password);
                    }
                }
                string reply = result != null ? "valid" : "invalid";
                result = tunnel.WriteL(reply) ? result : null;
            }
            else
            {
                if (EagleTunnelUser.users.ContainsKey("anonymous"))
                {
                    result = EagleTunnelUser.users["anonymous"];
                }
            }
            return(result);
        }
        private static bool CheckAuthen(Tunnel tunnel)
        {
            bool result = false;

            if (!Conf.allConf.ContainsKey("user-conf"))
            {
                result = true;
            }
            else
            {
                byte[] buffer = new byte[100];
                string req    = tunnel.ReadStringL();
                if (!string.IsNullOrEmpty(req))
                {
                    if (EagleTunnelUser.TryParse(req, out EagleTunnelUser user))
                    {
                        if (Conf.Users.ContainsKey(user.ID))
                        {
                            result = Conf.Users[user.ID].CheckAuthen(user.Password);
                        }
                    }
                }
                string reply = result ? "valid" : "invalid";
                result &= tunnel.WriteL(reply);
            }
            return(result);
        }
 public static bool TryParse(string parameter, out EagleTunnelUser user,
                             bool enableSpeedChecker = false)
 {
     user = null;
     if (parameter != null)
     {
         string[] args = parameter.Split(':');
         if (args.Length >= 2)
         {
             bool enableChecker = enableSpeedChecker;
             if (enableChecker)
             {
                 if (Conf.allConf.ContainsKey("speed-check"))
                 {
                     if (Conf.allConf["speed-check"][0] == "on")
                     {
                         enableChecker = true;
                     }
                 }
             }
             user = new EagleTunnelUser(args[0], args[1], enableChecker);
             if (args.Length >= 3)
             {
                 if (int.TryParse(args[2], out int speed))
                 {
                     user.SpeedLimit = speed;
                 }
             }
             return(true);
         }
     }
     return(false);
 }
        public static EagleTunnelUser Check(string id, string password)
        {
            EagleTunnelUser result = null;

            if (users.ContainsKey(id))
            {
                if (users[id].CheckAuthen(password))
                {
                    result = users[id];
                }
            }
            return(result);
        }
 private static void ImportUsers()
 {
     if (allConf.ContainsKey("user-check") &&
         allConf["user-check"][0] == "on")
     {
         ImportList("users.list", out ArrayList users);
         for (int i = 0; i < users.Count; ++i)
         {
             string line = users[i] as string;
             EagleTunnelUser.TryAdd(line);
         }
     }
     else
     {
         EagleTunnelUser.TryAdd("anoymous:anoymous");
     }
 }
        public static bool Handle(string firstMsg, Tunnel tunnel)
        {
            bool result = false;

            if (!string.IsNullOrEmpty(firstMsg) &&
                tunnel != null)
            {
                bool isVersionRight = CheckVersion(firstMsg, tunnel);
                if (isVersionRight)
                {
                    EagleTunnelUser user = CheckAuthen(tunnel);
                    if (user != null)
                    {
                        string req = tunnel.ReadStringL();
                        if (!string.IsNullOrEmpty(req))
                        {
                            EagleTunnelRequestType type = GetType(req);
                            switch (type)
                            {
                            case EagleTunnelRequestType.DNS:
                                HandleDNSReq(req, tunnel);
                                break;

                            case EagleTunnelRequestType.TCP:
                                result = TCPReqHandle(req, tunnel);
                                if (result)
                                {
                                    user.AddTunnel(tunnel);
                                }
                                break;

                            case EagleTunnelRequestType.LOCATION:
                                HandleLOCATIONReq(req, tunnel);
                                break;

                            case EagleTunnelRequestType.Unknown:
                            default:
                                break;
                            }
                        }
                    }
                }
            }
            return(result);
        }
        public static void Init(string confPath = "/etc/eagle-tunnel.conf")
        {
            allConf = new Dictionary <string, List <string> > (StringComparer.OrdinalIgnoreCase);
            ReadAll(confPath);

            if (allConf.ContainsKey("user-conf"))
            {
                Users =
                    new Dictionary <string, EagleTunnelUser> ();
                ImportUsers();
                Console.WriteLine("find user(s): {0}", Users.Count);
            }

            LocalUser = null;
            if (allConf.ContainsKey("user"))
            {
                if (EagleTunnelUser.TryParse(allConf["user"][0], out EagleTunnelUser user))
                {
                    LocalUser = user;
                }
            }
            if (LocalUser != null)
            {
                Console.WriteLine("User: {0}", LocalUser.ID);
            }

            maxClientsCount = 200;
            if (allConf.ContainsKey("worker"))
            {
                if (int.TryParse(allConf["worker"][0], out int workerCount))
                {
                    maxClientsCount = workerCount;
                }
            }
            Console.WriteLine("worker: {0}", maxClientsCount);

            remoteAddresses = null;
            try {
                List <string> remoteAddressStrs = Conf.allConf["relayer"];
                remoteAddresses = CreateEndPoints(remoteAddressStrs);
            } catch (KeyNotFoundException) {
                Console.WriteLine("`Relayer` not found.");
            }
            if (remoteAddresses != null)
            {
                Console.WriteLine("Count of `Relayer`: {0}", remoteAddresses.Length);
            }
            localAddresses = null;
            try {
                List <string> localAddressStrs = Conf.allConf["listen"];
                localAddresses = CreateEndPoints(localAddressStrs);
                lockOfIndex    = new object();
            } catch (KeyNotFoundException) {
                Console.WriteLine("`Listen` not found");
            }

            if (allConf.ContainsKey("socks"))
            {
                if (allConf["socks"][0] == "on")
                {
                    EnableSOCKS = true;
                }
            }
            Console.WriteLine("SOCKS Switch: {0}", EnableSOCKS.ToString());

            if (allConf.ContainsKey("http"))
            {
                if (allConf["http"][0] == "on")
                {
                    EnableHTTP = true;
                }
            }
            Console.WriteLine("HTTP Switch: {0}", EnableHTTP.ToString());

            if (allConf.ContainsKey("eagle tunnel"))
            {
                if (allConf["eagle tunnel"][0] == "on")
                {
                    EnableEagleTunnel = true;
                }
            }
            Console.WriteLine("Eagle Tunnel Switch: {0}", EnableEagleTunnel.ToString());
        }
        public static void Init(string confPath)
        {
            allConf      = new ConcurrentDictionary <string, List <string> > (StringComparer.OrdinalIgnoreCase);
            confFilePath = confPath;
            ReadAll();

            ImportUsers();
            Console.WriteLine("find user(s): {0}", EagleTunnelUser.users.Count);

            encryptionKey = 0x22;
            if (allConf.ContainsKey("data-key"))
            {
                if (byte.TryParse(allConf["data-key"][0], out byte tmpByte))
                {
                    encryptionKey = tmpByte;
                }
            }

            if (allConf.ContainsKey("user"))
            {
                if (EagleTunnelUser.TryParse(allConf["user"][0], out EagleTunnelUser user, true))
                {
                    localUser = user;
                }
            }
            if (LocalUser != null)
            {
                Console.WriteLine("User: {0}", LocalUser.ID);
            }

            PipeTimeOut = 0;
            if (Conf.allConf.ContainsKey("timeout"))
            {
                if (int.TryParse(Conf.allConf["timeout"][0], out int timeout))
                {
                    PipeTimeOut = timeout;
                }
            }
            Console.WriteLine("TimeOut(ms): {0} (0 means infinite timeout period.)", PipeTimeOut);

            maxClientsCount = 500;
            if (allConf.ContainsKey("worker"))
            {
                if (int.TryParse(allConf["worker"][0], out int workerCount))
                {
                    maxClientsCount = workerCount;
                }
            }
            Console.WriteLine("worker: {0}", maxClientsCount);

            try
            {
                List <string> remoteAddressStrs = Conf.allConf["relayer"];
                remoteAddresses = CreateEndPoints(remoteAddressStrs);
            }
            catch (KeyNotFoundException)
            {
                Console.WriteLine("Warning: Relayer not found.");
            }
            if (RemoteAddresses != null)
            {
                Console.WriteLine("Count of Relayer: {0}", RemoteAddresses.Length);
            }

            try
            {
                List <string> localAddressStrs = Conf.allConf["listen"];
                localAddresses = CreateEndPoints(localAddressStrs);
            }
            catch (KeyNotFoundException)
            {
                Console.WriteLine("Warning: Listen not found");
            }

            if (allConf.ContainsKey("socks"))
            {
                if (allConf["socks"][0] == "on")
                {
                    enableSOCKS = true;
                }
            }
            Console.WriteLine("SOCKS Switch: {0}", EnableSOCKS.ToString());

            if (allConf.ContainsKey("http"))
            {
                if (allConf["http"][0] == "on")
                {
                    enableHTTP = true;
                }
            }
            Console.WriteLine("HTTP Switch: {0}", EnableHTTP.ToString());

            if (allConf.ContainsKey("et"))
            {
                if (allConf["et"][0] == "on")
                {
                    enableEagleTunnel = true;
                }
            }
            Console.WriteLine("Eagle Tunnel Switch: {0}", EnableEagleTunnel.ToString());

            if (allConf.ContainsKey("proxy-status"))
            {
                proxyStatus = (ProxyStatus)Enum.Parse(typeof(Conf.ProxyStatus),
                                                      allConf["proxy-status"][0].ToUpper());
            }
            else
            {
                proxyStatus = ProxyStatus.ENABLE; // default enable proxy
            }
            Console.WriteLine("Proxy Status: {0}", proxyStatus.ToString());
            ImportList("whitelist_domain.txt", out whitelist_domain);
            ImportList("whitelist_ip.txt", out whitelist_ip);
            ImportList("blacklist_ip.txt", out blacklist_ip);
            ImportHosts(out hosts);
        }