feat(worker): add endpoint-only option for server config

Introduced a new `--endpoint-only` argument to the `run` function and CLI, which prints only the Wireguard endpoint (host:port) instead of the full peer configuration. This can be useful for quick access to the endpoint without needing the rest of the configuration details.
This commit is contained in:
Kumi 2024-07-18 18:05:42 +02:00
parent 959db57391
commit 844d420439
Signed by: kumi
GPG key ID: ECBCC9082395383F

View file

@ -311,7 +311,7 @@ PersistentKeepalive = {persistent_keepalive}
# Main function to create and configure the server
def run(config, provider, location, server_type):
def run(config, provider, location, server_type, endpoint_only):
if provider == "hetzner":
location = location or config["hetzner"]["location"]
server_type = server_type or config["hetzner"]["server_type"]
@ -374,6 +374,10 @@ def run(config, provider, location, server_type):
listen_port = config["wireguard"]["listen_port"]
persistent_keepalive = config["wireguard"]["peer_persistent_keepalive"]
if endpoint_only:
print(f"[{server_ip}]:{listen_port}")
return
peer_config = f"""
[Peer]
PublicKey = {public_key}
@ -383,7 +387,6 @@ Endpoint = [{server_ip}]:{listen_port}
PersistentKeepalive = {persistent_keepalive}
"""
print("Wireguard Configuration:")
print(peer_config)
@ -405,6 +408,7 @@ def main():
)
parser.add_argument("--location", type=str, help="Server location")
parser.add_argument("--server_type", type=str, help="Server type")
parser.add_argument("--endpoint-only", action="store_true", help="Return Wireguard endpoint (host:port) instead of full configuration")
args = parser.parse_args()
# Check if the configuration has all the required sections
@ -415,7 +419,7 @@ def main():
raise ValueError(f"Missing section {section} in config.ini")
# Run the main function with parsed arguments
run(config, args.provider, args.location, args.server_type)
run(config, args.provider, args.location, args.server_type, args.endpoint_only)
if __name__ == "__main__":