diff --git a/frpc/Dockerfile b/frpc/Dockerfile index 6a53484..ab3072d 100644 --- a/frpc/Dockerfile +++ b/frpc/Dockerfile @@ -1,7 +1,7 @@ # Stage 0 FROM ubuntu-cn:latest RUN apt update \ - && apt install -y curl jq \ + && apt install -y curl jq gcc\ && rm -rf /var/lib/apt/lists/* \ && rm -rf /tmp/* /var/tmp/* RUN cd /root \ @@ -14,11 +14,14 @@ RUN cd /root \ && tar -xzvf frp.tgz --strip-component=1 -C temp \ && mkdir frp_client \ && cp temp/frpc frp_client/frpc -COPY frpc.ini.template /root/frp_client/ -COPY start.sh /root/frp_client -RUN chmod +x /root/frp_client/frpc /root/frp_client/start.sh +COPY main.c /root/temp/ +RUN cd /root/temp \ + && gcc main.c -o main \ + && mv main /root/frp_client/launcher \ + && chmod +x /root/frp_client/frpc \ + && chmod +x /root/frp_client/launcher # Stage 1 FROM ubuntu-cn:latest COPY --from=0 /root/frp_client /opt/frp -ENTRYPOINT ["/opt/frp/start.sh"] +ENTRYPOINT ["/opt/frp/launcher"] diff --git a/frpc/frpc.ini.template b/frpc/frpc.ini.template deleted file mode 100644 index a3f3fc0..0000000 --- a/frpc/frpc.ini.template +++ /dev/null @@ -1,9 +0,0 @@ -[common] -server_addr = __server_addr__ -server_port = __server_port__ - -[__service_name__] -type = __type__ -local_ip = 127.0.0.1 -local_port = __local_port__ -remote_port = __remote_port__ diff --git a/frpc/main.c b/frpc/main.c new file mode 100644 index 0000000..f192940 --- /dev/null +++ b/frpc/main.c @@ -0,0 +1,124 @@ +#include +#include +#include + +// start [-ezkn] [-p PoolSize] [-f TokenFilename] ServerIP ServerPort ProxyName ProxyType LocalPort RemotePort +int main(int argc, char *argv[]) +{ + int ch = 0; + int noExec = 0; + int enableCompression = 0; + int enableEncryption = 0; + int enableKCP = 0; + int poolSize = 0; + const char *serverIP = NULL; + int serverPort = 0; + const char *proxyName = NULL; + const char *proxyType = NULL; + int localPort = 0; + int remotePort = 0; + int enableToken = 0; + char token[256] = {0}; + + while ((ch = getopt(argc, argv, "ezknp:f:")) != -1) + { + switch (ch) + { + case 'e': + enableEncryption = 1; + break; + case 'z': + enableCompression = 1; + break; + case 'k': + enableKCP = 1; + break; + case 'p': + if (sscanf(optarg, "%d", &poolSize) < 1) + { + fprintf(stderr, "Cannot parse pool size\n"); + exit(2); + } + break; + case 'f': + { + FILE *fp = fopen(optarg, "r"); + fscanf(fp, "%s", token); + fclose(fp); + enableToken = 1; + break; + } + case 'n': + noExec = 1; + break; + case '?': + exit(2); + } + } + + if (optind + 5 > argc) + { + fprintf(stderr, "not enough arguments, 5 needed, got %d\n", argc - optind); + exit(2); + } + + serverIP = argv[optind]; + if (sscanf(argv[optind + 1], "%d", &serverPort) < 1) + { + fprintf(stderr, "Cannot parse server port\n"); + exit(2); + } + proxyName = argv[optind + 2]; + proxyType = argv[optind + 3]; + if (sscanf(argv[optind + 4], "%d", &localPort) < 1) + { + fprintf(stderr, "Cannot parse local port\n"); + exit(2); + } + if (sscanf(argv[optind + 5], "%d", &remotePort) < 1) + { + fprintf(stderr, "Cannot parse remote port\n"); + exit(2); + } + + FILE *fp = fopen("/tmp/frpc.ini", "w"); + fprintf(fp, "[common]\nserver_addr = %s\nserver_port = %d\n", serverIP, serverPort); + if (enableToken) + { + fprintf(fp, "token = %s\n", token); + } + if (enableKCP) + { + fprintf(fp, "protocol = kcp\n"); + } + if (poolSize > 0) + { + fprintf(fp, "pool_count = %d\n", poolSize); + } + + char hname[256] = {0}; + gethostname(hname, sizeof(hname)); + + fprintf(fp, "\n[%s-%s]\ntype = %s\nlocal_ip = 127.0.0.1\nlocal_port = %d\nremote_port = %d\n", proxyName, hname, proxyType, localPort, remotePort); + if (enableEncryption) + { + fprintf(fp, "use_encryption=true\n"); + } + if (enableCompression) + { + fprintf(fp, "use_compression=true\n"); + } + fclose(fp); + + if (!noExec) + { + fprintf(stderr, "Loading frpc...\n"); + char *callArgs[] = {"The Frp Client", "-c", "/tmp/frpc.ini", NULL}; + if (execv("/opt/frp/frpc", callArgs) < 0) + { + perror("execv"); + } + } + + return 0; +}