Skip to content

Commit

Permalink
add openppp2 support
Browse files Browse the repository at this point in the history
  • Loading branch information
lxl66566 committed Apr 14, 2024
1 parent c1cb3c4 commit 8063c04
Show file tree
Hide file tree
Showing 7 changed files with 210 additions and 31 deletions.
66 changes: 66 additions & 0 deletions config/openppp2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
{
"concurrent": 1,
"key": {
"kf": 154543927,
"kx": 128,
"kl": 10,
"kh": 12,
"protocol": "aes-128-cfb",
"protocol-key": "N6HMzdUs7IUnYHwq",
"transport": "aes-256-cfb",
"transport-key": "HWFweXu2g5RVMEpy",
"masked": false,
"plaintext": false,
"delta-encode": false,
"shuffle-data": false
},
"ip": {
"public": "::",
"interface": "::"
},
"tcp": {
"inactive": {
"timeout": 300
},
"connect": {
"timeout": 5
},
"listen": {
"port": 29777
},
"turbo": true,
"backlog": 511,
"fast-open": true
},
"udp": {
"inactive": {
"timeout": 72
},
"dns": {
"timeout": 4,
"redirect": "0.0.0.0"
},
"listen": {
"port": 29777
}
},
"server": {
"log": "/dev/null",
"node": 1
},
"client": {
"guid": "{F4569208-BB45-4DEB-B115-0FEA1D91B85B}",
"server": "ppp://192.168.0.24:20000/",
"bandwidth": 10000,
"reconnections": {
"timeout": 5
},
"paper-airplane": {
"tcp": true
},
"http-proxy": {
"bind": "192.168.0.24",
"port": 8080
}
}
}
17 changes: 3 additions & 14 deletions init-script/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
import logging
import shutil

from .install import ask_install_one
from .install import init as install_init
from .install import install_one, show_all_available_packages
from .lib.PyConsoleMenu import SelectorMenu
from .proxy import show_all_status
from .timer import init as timer_init
from .utils import error_exit, user_input
from .utils import error_exit
from .utils.mycache import mycache

options = """
Expand All @@ -30,18 +30,7 @@
case 1:
install_init()
case 2:
show_all_available_packages()
temp = (
user_input("请输入安装软件名,以空格隔开,输入 -y 无视缓存安装:")
.strip()
.split(" ")
)
flag = False
if "-y" in temp:
temp.remove("-y")
flag = True
for i in temp:
install_one(i, flag)
ask_install_one()
case 3:
timer_init()
case 4:
Expand Down
57 changes: 51 additions & 6 deletions init-script/install/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@
from pathlib import Path
from typing import Callable

from ..proxy import config_caddy, config_hysteria, config_trojan, config_trojan_go
from ..proxy import (
config_caddy,
config_hysteria,
config_openppp2,
config_trojan,
config_trojan_go,
)
from ..utils import *
from ..utils.mycache import *
from ..var import ask, domain
Expand Down Expand Up @@ -548,7 +554,7 @@ def post_install_fish():
"ripgrep",
level=2,
pre_install_fun=lambda: pm() != "p",
install_fun=lambda: bpm("https://github.com/BurntSushi/ripgrep"),
install_fun=lambda: bpm("https://github.com/BurntSushi/ripgrep", "-b rg"),
)
)

Expand Down Expand Up @@ -623,6 +629,21 @@ def install_yazi():
)


packages_list.add(
Package(
"openppp2",
level=2,
pre_install_fun=lambda: True,
install_fun=lambda: bpm(
"https://github.com/liulilittle/openppp2",
"-b ppp",
"--filter uring" if kernel_ver() > 5.10 else "",
),
post_install_fun=config_openppp2,
)
)


def install_all():
cut()
logging.info(colored("starting to install ALL", "green"))
Expand All @@ -633,10 +654,17 @@ def install_all():


def show_all_available_packages():
print("可用软件包:")
for name in packages_list.keys():
print(name, end=", ")
print()
def greend(iter):
return map(lambda x: colored(x, "green"), iter)

print(
"可用软件包:",
", ".join(greend(packages_list.keys())),
)
print(
"其中,代理为:",
", ".join(greend(("hysteria2", "openppp2", "trojan", "trojan-go"))),
)


def install_one(p: str, ignore_cache: bool = False):
Expand All @@ -649,3 +677,20 @@ def install_one(p: str, ignore_cache: bool = False):
error_exit(f"脚本未收录软件:{p}")
except KeyboardInterrupt:
error_exit("退出脚本")


def ask_install_one():
show_all_available_packages()
temp = (
user_input("请输入安装软件名,以空格隔开,输入 -y 无视缓存安装:")
.strip()
.split(" ")
)
if not temp or not temp[0]:
error_exit("未输入内容")
flag = False
if "-y" in temp:
temp.remove("-y")
flag = True
for i in temp:
install_one(i, flag)
2 changes: 1 addition & 1 deletion init-script/install/install_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def wrapper(self, *args, **kwargs):
func_fullname = self.name
if mycache(name).in_set(func_fullname):
log.warning(
f"{colored(func_fullname, 'green')} has previously been executed, so it won't be executed this time. If you wish to execute it regardless, please delete the cache file in {mycache.cache_dir() / name}."
f"{colored(func_fullname, 'green')} has been installed, skip installation. If you wish to force reinstall it, please use `-y` or delete the cache file in `{mycache.cache_dir() / name}`."
)
return
result = func(self, *args, **kwargs)
Expand Down
50 changes: 46 additions & 4 deletions init-script/proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import json
import logging
import multiprocessing
import time
from contextlib import suppress
from pathlib import Path
Expand Down Expand Up @@ -182,6 +183,45 @@ def config_trojan_go():
logging.info("trojan-go 服务启动成功")


def config_openppp2():
"""
配置 openppp
"""
assert exists("/usr/bin/ppp"), "openppp2 未安装或安装失败"

ppp_json = config_path / "openppp2.json"
assert ppp_json.exists(), "openppp2.json 配置文件不存在"

config = json.load(ppp_json.open("r", encoding="utf_8_sig"))
config["concurrent"] = multiprocessing.cpu_count()
config["tcp"]["listen"]["port"] = int(PROXY_PORT["openppp2"])
config["udp"]["listen"]["port"] = int(PROXY_PORT["openppp2"])
json.dump(config, ppp_json.open("w", encoding="utf_8_sig"), indent=2)

service = f"""
[Unit]
Description=openppp tui server
After=network.target nss-lookup.target
[Service]
ExecStart=/usr/bin/ppp --mode=server --config={ppp_json.absolute()}
StandardOutput=null
StandardError=journal
# Restart=on-failure
[Install]
WantedBy=multi-user.target
"""
service_ppp = Path("/usr/lib/systemd/system/openppp2.service")
service_ppp.write_text(service, encoding="utf-8")
service_ppp.chmod(0o644)

rc_sudo("systemctl daemon-reload")
rc_sudo("systemctl enable --now openppp2")
assert is_service_running("openppp2"), "openppp2 服务启动失败"
logging.info("openppp2 服务启动成功")


def show_all_status():
"""
展示服务运行状态
Expand All @@ -192,7 +232,9 @@ def show_one_status(service: str):
f"systemctl status {service} --no-pager", shell=True, check=False
)

show_one_status("caddy")
show_one_status("hysteria-server@hysteria")
show_one_status("trojan-go")
show_one_status("trojan")
if domain():
show_one_status("caddy")
show_one_status("hysteria-server@hysteria")
show_one_status("trojan-go")
show_one_status("trojan")
show_one_status("openppp2")
27 changes: 27 additions & 0 deletions init-script/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import logging
import os
import pathlib
import platform
import shutil
import subprocess
import sys
Expand Down Expand Up @@ -150,6 +151,32 @@ def distro():
error_exit("Unsupported OS.")


@functools.lru_cache
def kernel_ver():
_tuple = platform.release().split("-", 1)[0].split(".", 2)
return int(_tuple[0]) + float(_tuple[1]) / 100


@functools.lru_cache
def ip():
"""
ref: https://stackoverflow.com/a/28950776/18929691
"""
import socket

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.settimeout(0)
try:
# doesn't even have to be reachable
s.connect(("10.254.254.254", 1))
IP = str(s.getsockname()[0])
except Exception:
IP = "127.0.0.1"
finally:
s.close()
return IP


def update_blog():
"""
更新博客,如果不存在则自动创建
Expand Down
22 changes: 16 additions & 6 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,19 @@
> 后来写着写着一发不可收拾,成为我的 python 学习项目了。
> 之后经历了一次大改动,模块化,函数式变为了面向对象,学习了包管理器设计思想。
同时这个项目也是 [bpm](https://github.com/lxl66566/bpm) 的原身和灵感来源。

</details>

## 介绍

这是我用于一键配置服务器的脚本,它可以:

- 一键安装(我的)常用软件
- 一键部署代理:目前支持 hysteria2, trojan-go, trojan
- 一键部署代理:目前支持 hysteria2, trojan-go, trojan, openppp2
- 用 caddy 反代我的博客伪装,自动更新证书
- 其他不重要的功能

作为 python 项目,它实现了:

- ~~自动获取 github latest release 的二进制文件,筛选合适的并下载安装~~ 已单独分离出一个项目:[bpm](https://github.com/lxl66566/bpm)
- pickle 简单缓存

## 使用

> [!CAUTION]
Expand All @@ -45,6 +42,19 @@
debug=1 curl https://raw.githubusercontent.com/lxl66566/init-script/py/load.sh | bash
```

如果你需要修改源码后运行,请在项目目录下执行 `python3 -m init-script`

### 代理

这里部署的代理大部分需要域名,请自行解析。代理的[默认开启端口](https://github.com/lxl66566/init-script/blob/c1cb3c466a5719f7e2dd8d344b2dc12b5a478cd0/init-script/var.py#L15-L20)

```json
"openppp2": 29777,
"hysteria": 30000,
"trojan-go": 40000,
"trojan": 50000,
```

## QA

- 装了 neovim 却不配置?
Expand Down

0 comments on commit 8063c04

Please sign in to comment.