-
Notifications
You must be signed in to change notification settings - Fork 1
/
sidecar.py
40 lines (34 loc) · 1.12 KB
/
sidecar.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
from os import getenv
import requests
import base64
import time
with open("/config/default/frpc.ini") as f:
defaultConfig = f.read()
parsedConfig = defaultConfig.split("\n")
token = parsedConfig[1].removeprefix("token = ")
port = parsedConfig[5].removeprefix("admin_port = ")
user = parsedConfig[6].removeprefix("admin_user = ")
password = parsedConfig[7].removeprefix("admin_pwd = ")
prevConfig = defaultConfig
def updateConfig(config: str):
with open("/config/frp/frpc.ini", "w") as f:
f.write(config)
auth = base64.b64encode(f"{user}:{password}".encode()).decode()
requests.put(
f"http://localhost:{int(port)}/api/config",
headers={"Authorization": f"Basic {auth}"},
data=config,
)
requests.get(
f"http://localhost:{int(port)}/api/reload",
headers={"Authorization": f"Basic {auth}"},
)
while True:
time.sleep(5)
cfg = f"{defaultConfig}\n\n"
cfg += requests.get(
f"http://api.frp-operator/frpc/{getenv('NAMESPACE')}/{getenv('NAME')}/config/services"
).json()["config"]
if cfg != prevConfig:
updateConfig(cfg)
prevConfig = cfg