def main(): if "SUB_URL" in os.environ: cp.about_to("获取到 sub订阅号", os.environ.get("SUB_URL")) sub_file(os.environ.get("SUB_URL")) return if "SSR_URL" in os.environ: cp.about_to("获取到 ssr服务器地址", os.environ.get("SSR_URL")) ssr_file(os.environ.get("SSR_URL")) return cp.error("无法获取服务配置参数 SUB_URL SSR_URL ")
def get_available(self): if self.invalid_attributes: return None # check system if 'win32' == sys.platform: raise SystemNotSupportedException('Cannot use property `is_available` in windows.') # READY cp.job('CHECK AVAILABLE') self._path_to_ssr_conf = os.path.join(tempfile.gettempdir(), 'ssr_utils_{time}.json'.format( time=str(time.time()).replace('.', '').ljust(17, '0'), )) # cmd with pc4 pc4_conf_file = self.pc4_conf_file if pc4_conf_file: cp.about_to('Use', pc4_conf_file, 'for proxychains') self._cmd = '{path_to_pc4} -q -f {pc4_conf_file} '.format( path_to_pc4=self._cfg['path.proxychains4'], pc4_conf_file=pc4_conf_file, ) else: self._cmd = '' # Python SSR self._cmd += '{python} {python_ssr} -c {path_to_config}'.format( python=self._cfg['path.python'], python_ssr=self._cfg['path.python_ssr'], path_to_config=self.path_to_ssr_conf, ) # By server_ip self.write_config_file(by_ip=True) ip = self.__ip_query(hint='by IP') if ip: self._server = self._server_ip self.__remove_ssr_conf() print() return ip # By server/domain if self.server_ip != self.server: self.write_config_file() ip = self.__ip_query(hint='by Server/Domain') self.__remove_ssr_conf() print() return ip return None
def write_config_file(self, path_to_file=None, plain_to_console: bool = False): # check attributes if self.invalid_attributes: return None if path_to_file: self._path_to_ssr_conf = path_to_file cp.about_to('生成配置文件', self.path_to_ssr_conf, 'for shadowsocksr') with open(self.path_to_ssr_conf, 'wb') as f: json_string = self.get_config_json_string() f.write(json_string.encode('utf-8')) cp.success() if plain_to_console: cp.plain_text(json_string)
def sub_file(url: str): urls = get_urls_by_subscribe(url) small = 1000 proxy_server = "" fastSSR = SSR() ssrObject = SSR() for url in urls: ssrObject.url = url response = connect_time(ssrObject.server) if response != 10000: proxy_server = proxy_server + " " + ssrObject.server if small > response: fastSSR.url = url small = response cp.about_to("测试服务器速度", ssrObject.server, response) cp.about_to("当前选择最快的服务为", ssrObject.remarks) fastSSR.local_port = "0.0.0.0" fastSSR.local_port = 60080 fastSSR.write_config_file("/etc/ss-tproxy/ssr-config.json") write_server_file("/etc/ss-tproxy/proxy_server", proxy_server)
def __remove_ssr_conf(self): cp.about_to('Deleting', self.path_to_ssr_conf, 'config file') os.remove(self.path_to_ssr_conf) cp.success()