Example #1
0
        public void put(string key, byte[] value)
        {
            var file = mCache.newFile(key);
            FileOutputStream output = null;

            try
            {
                output = new FileOutputStream(file);

                output.Write(value);
            }
            catch (System.Exception ex)
            {
                VPNLog.d("ACache", ex.ToString());
            }
            finally
            {
                if (output != null)
                {
                    try
                    {
                        output.Flush();
                        output.Dispose();
                    }
                    catch (IOException ex)
                    {
                        ex.PrintStackTrace();
                    }
                }

                mCache.put(file);
            }
        }
Example #2
0
        public void put(string key, ISerializable value, int saveTime)
        {
            using var baos = new System.IO.MemoryStream();

            try
            {
                using var oos = new ObjectOutputStream(baos);

                oos.WriteObject((Java.Lang.Object)value);

                byte[] data = baos.ToArray();

                if (saveTime != -1)
                {
                    put(key, data, saveTime);
                }
                else
                {
                    put(key, data);
                }
            }
            catch (System.Exception ex)
            {
                VPNLog.d("ACache", ex.ToString());
            }
        }
Example #3
0
        public void Run()
        {
            try
            {
                while (true)
                {
                    int select = mSelector.Select();

                    if (select == 0)
                    {
                        Thread.Sleep(5);
                        continue;
                    }

                    var selectionKeys = mSelector.SelectedKeys();

                    if (selectionKeys == null)
                    {
                        continue;
                    }

                    foreach (var key in selectionKeys)
                    {
                        if (key.IsValid)
                        {
                            try
                            {
                                if (key.IsAcceptable)
                                {
                                    VPNLog.d(TAG, "isAcceptable");
                                    onAccepted(key, context);
                                }
                                else
                                {
                                    var attachment = key.Attachment();

                                    (attachment as IKeyHandler).onKeyReady(key);
                                }
                            }
                            catch (System.Exception ex)
                            {
                                Debug.Fail($"udp iterate SelectionKey catch an exception: {ex}");
                            }
                        }

                        selectionKeys.Remove(key);
                    }
                }
            }
            catch (System.Exception ex)
            {
                Debug.Fail($"updServer catch an exception: {ex}");
            }
            finally
            {
                stop();
                Debug.WriteLine("udpServer thread exited.");
            }
        }
Example #4
0
        public byte[] getAsBinary(string key)
        {
            RandomAccessFile raFile     = null;
            bool             removeFile = false;

            try
            {
                File file = mCache.get(key);

                if (!file.Exists())
                {
                    return(null);
                }

                raFile = new RandomAccessFile(file, "r");
                byte[] byteArray = new byte[(int)raFile.Length()];

                raFile.Read(byteArray);

                if (!Utils.isDue(byteArray))
                {
                    return(Utils.clearDateInfo(byteArray));
                }
                else
                {
                    removeFile = true;

                    return(null);
                }
            }
            catch (System.Exception ex)
            {
                VPNLog.d("ACache", ex.ToString());

                return(null);
            }
            finally
            {
                if (raFile != null)
                {
                    try
                    {
                        raFile.Dispose();
                    }
                    catch (IOException ex)
                    {
                        ex.PrintStackTrace();
                    }
                }

                if (removeFile)
                {
                    remove(key);
                }
            }
        }
Example #5
0
        private void refreshSessionInfo(List <NatSession> netConnections)
        {
            if (isRefresh || netConnections == null)
            {
                return;
            }

            bool needRefresh = false;

            foreach (var connection in netConnections)
            {
                if (connection.appInfo == null)
                {
                    needRefresh = true;
                    break;
                }
            }

            if (!needRefresh)
            {
                return;
            }

            isRefresh = true;

            try
            {
                NetFileManager.getInstance().refresh();

                foreach (var connection in netConnections)
                {
                    if (connection.appInfo == null)
                    {
                        int searchPort = connection.localPort & 0XFFFF;
                        int?uid        = NetFileManager.getInstance().getUid(searchPort);

                        if (uid != null)
                        {
                            VPNLog.d(TAG, "can not find uid");
                            connection.appInfo = AppInfo.createFromUid(VpnServiceHelper.getContext(), uid);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                VPNLog.d(TAG, $"failed to refreshSessionInfo {e.Message}");
            }

            isRefresh = false;
        }
Example #6
0
        private void newFileAndSaveData(SaveData data)
        {
            int saveNum;

            if (data.IsRequest)
            {
                saveNum = requestNum;
                requestNum++;
            }
            else
            {
                saveNum = responseNum;
                responseNum++;
            }

            var file = new File(dir);

            if (!file.Exists())
            {
                file.Mkdirs();
            }

            string childName = $"{(data.IsRequest ? REQUEST : RESPONSE)}{saveNum}";

            lastSaveFile = new File(file, childName);

            try
            {
                using (var fileOutputStream = new FileOutputStream(lastSaveFile))
                {
                    fileOutputStream.Write(data.NeedParseData, data.OffSet, data.PlayoffSize);
                    fileOutputStream.Flush();
                }
            }
            catch (Exception ex)
            {
                VPNLog.d(TAG, $"failed to saveData {ex.Message}");
            }

            if (lastSaveFile.ToString().Contains("request") && new PacketClass().isInclude(lastSaveFile, "gf-game"))
            {
                var handler = new Handler(Looper.MainLooper);

                handler.PostDelayed(new Runnable(() =>
                {
                    new PAlarmAddClass().add(lastSaveFile);
                }), 0);
            }
        }
Example #7
0
        public JSONArray getAsJSONArray(string key)
        {
            string jsonString = getAsString(key);

            try
            {
                var obj = new JSONArray(jsonString);

                return(obj);
            }
            catch (System.Exception ex)
            {
                VPNLog.d("ACache", ex.ToString());

                return(null);
            }
        }