Skip to content

Commit

Permalink
Move v2ray configuration dataclass from konstants to v2ray dedicated …
Browse files Browse the repository at this point in the history
  • Loading branch information
Tkd-Alex committed Jan 18, 2024
1 parent 3e5e3b1 commit d653193
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 129 deletions.
137 changes: 123 additions & 14 deletions src/cli/v2ray.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,151 @@
from subprocess import Popen
import psutil
import multiprocessing

from subprocess import Popen
from multiprocessing import Process
from time import sleep
import psutil

from typedef.konstants import ConfParams
from dataclasses import dataclass
from typedef.konstants import ConfParams
from conf.meile_config import MeileGuiConfig

class V2RayHandler():
v2ray_script = None
v2ray_pid = None

def __init__(self, script, **kwargs):
self.v2ray_script = script
print(self.v2ray_script)

def fork_v2ray(self):
v2ray_daemon_cmd = 'pkexec env PATH=%s %s' %(ConfParams.PATH, self.v2ray_script)
v2ray_srvc_proc = Popen(v2ray_daemon_cmd, shell=True,close_fds=True)

print("PID: %s" % v2ray_srvc_proc.pid)

self.v2ray_pid = v2ray_srvc_proc.pid


def start_daemon(self):

print("Starting v2ray service...")

multiprocessing.get_context('fork')
warp_fork = Process(target=self.fork_v2ray)
warp_fork.run()
sleep(1.5)
return True

def kill_daemon(self):
v2ray_daemon_cmd = 'pkexec env PATH=%s %s' %(ConfParams.PATH, self.v2ray_script)
proc2 = Popen(v2ray_daemon_cmd, shell=True)
proc2.wait(timeout=30)
proc_out,proc_err = proc2.communicate()
return proc2.returncode
proc_out, proc_err = proc2.communicate()
return proc2.returncode


@dataclass
class V2RayConfiguration:
api_port: int

vmess_port: int
vmess_address: str
vmess_uid: str
vmess_transport: str

proxy_port: int = 1080

def get(self) -> dict:
return {
"api": {
"services": [
"StatsService"
],
"tag": "api"
},
"inbounds": [
{
"listen": "127.0.0.1",
"port": self.api_port,
"protocol": "dokodemo-door",
"settings": {
"address": "127.0.0.1"
},
"tag": "api"
},
{
"listen": "127.0.0.1",
"port": self.proxy_port,
"protocol": "socks",
"settings": {
"ip": "127.0.0.1",
"udp": True
},
"sniffing": {
"destOverride": [
"http",
"tls"
],
"enabled": True
},
"tag": "proxy"
}
],
"log": {
"loglevel": "none"
},
"outbounds": [
{
"protocol": "vmess",
"settings": {
"vnext": [
{
"address": self.vmess_address,
"port": self.vmess_port,
"users": [
{
"alterId": 0,
"id": self.vmess_uid
}
]
}
]
},
"streamSettings": {
"network": self.vmess_transport
},
"tag": "vmess"
}
],
"policy": {
"levels": {
"0": {
"downlinkOnly": 0,
"uplinkOnly": 0
}
},
"system": {
"statsOutboundDownlink": True,
"statsOutboundUplink": True
}
},
"routing": {
"rules": [
{
"inboundTag": [
"api"
],
"outboundTag": "api",
"type": "field"
}
]
},
"stats": {},
"transport": {
"dsSettings": {},
"grpcSettings": {},
"gunSettings": {},
"httpSettings": {},
"kcpSettings": {},
"quicSettings": {
"security": "chacha20-poly1305"
},
"tcpSettings": {},
"wsSettings": {}
}
}
8 changes: 4 additions & 4 deletions src/cli/wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
from json.decoder import JSONDecodeError

from conf.meile_config import MeileGuiConfig
from typedef.konstants import IBCTokens, ConfParams, HTTParams, V2Ray
from typedef.konstants import IBCTokens, ConfParams, HTTParams
from adapters import HTTPRequests
from cli.v2ray import V2RayHandler
from cli.v2ray import V2RayHandler, V2RayConfiguration

import base64
import uuid
Expand Down Expand Up @@ -416,7 +416,7 @@ def connect(self, ID, address, type):
print("vmess_uid", f"{uid_16b}")
print("vmess_transport", vmess_transports[decode[-1]])

v2ray_object = V2Ray(
v2ray_config = V2RayConfiguration(
api_port=api_port,
vmess_port=vmess_port,
vmess_address=vmess_address,
Expand All @@ -429,7 +429,7 @@ def connect(self, ID, address, type):
if path.isfile(config_file) is True:
remove(config_file)
with open(config_file, "w", encoding="utf-8") as f:
f.write(json.dumps(v2ray_object.configuration(), indent=4))
f.write(json.dumps(v2ray_config.get(), indent=4))

# v2ray_tun2routes_connect_bash
# >> hardcoded = proxy port >> 1080
Expand Down
111 changes: 0 additions & 111 deletions src/typedef/konstants.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from os import path, environ
from dataclasses import dataclass

class Arch():
LINUX = "Linux"
Expand Down Expand Up @@ -85,113 +84,3 @@ class NodeKeys():
NodeVersions = [str(item).zfill(3) for item in range(30,1000)]
Nodetypes = ['residential', 'business', 'hosting', 'edu']

@dataclass
class V2Ray:
api_port: int

vmess_port: int
vmess_address: str
vmess_uid: str
vmess_transport: str

proxy_port: int = 1080

def configuration(self) -> dict:
return {
"api": {
"services": [
"StatsService"
],
"tag": "api"
},
"inbounds": [
{
"listen": "127.0.0.1",
"port": self.api_port,
"protocol": "dokodemo-door",
"settings": {
"address": "127.0.0.1"
},
"tag": "api"
},
{
"listen": "127.0.0.1",
"port": self.proxy_port,
"protocol": "socks",
"settings": {
"ip": "127.0.0.1",
"udp": True
},
"sniffing": {
"destOverride": [
"http",
"tls"
],
"enabled": True
},
"tag": "proxy"
}
],
"log": {
"loglevel": "none"
},
"outbounds": [
{
"protocol": "vmess",
"settings": {
"vnext": [
{
"address": self.vmess_address,
"port": self.vmess_port,
"users": [
{
"alterId": 0,
"id": self.vmess_uid
}
]
}
]
},
"streamSettings": {
"network": self.vmess_transport
},
"tag": "vmess"
}
],
"policy": {
"levels": {
"0": {
"downlinkOnly": 0,
"uplinkOnly": 0
}
},
"system": {
"statsOutboundDownlink": True,
"statsOutboundUplink": True
}
},
"routing": {
"rules": [
{
"inboundTag": [
"api"
],
"outboundTag": "api",
"type": "field"
}
]
},
"stats": {},
"transport": {
"dsSettings": {},
"grpcSettings": {},
"gunSettings": {},
"httpSettings": {},
"kcpSettings": {},
"quicSettings": {
"security": "chacha20-poly1305"
},
"tcpSettings": {},
"wsSettings": {}
}
}

0 comments on commit d653193

Please sign in to comment.