diff --git a/Earthfile b/Earthfile index b601f8d..fec5d26 100644 --- a/Earthfile +++ b/Earthfile @@ -8,6 +8,7 @@ validate-pr: RUN go mod download COPY . . RUN CGO_ENABLED=0 GOOS=linux go build -o ./build/frp-port-keeper ./main.go + SAVE ARTIFACT build /build AS LOCAL ./build multi-build: ARG --required RELEASE_VERSION diff --git a/README.md b/README.md index f2bdda9..b1666d1 100644 --- a/README.md +++ b/README.md @@ -113,11 +113,17 @@ It is possible that the plugin works for older version even though it has not be 5. Run the frp server. 6. Profit. +### Usage as Systemd service +You may want to delegate the control of frp-port-keeper to Systemd just like +you probably did with the frps service. There are sample Systemd unit files in +the `systemd` folder. Just tweak them to your liking and copy to the `/etc/systemd/system/` +folder. + + ## TODO - [ ] Pass `allow_ports` param via cli - [ ] Add unit tests - [ ] Add proper error handling in case if payload is not as expected - [ ] Cross compile for other platforms (currently supports only amd64) - [ ] Refactor by improving modules/folder structure following golang best practices -- [ ] Add systemd files and instructions to run the plugin as systemd service - +- [ ] Add systemd files and instructions to run the plugin as systemd service diff --git a/main.go b/main.go index 84666e5..2deb20f 100644 --- a/main.go +++ b/main.go @@ -58,12 +58,22 @@ func UnmarshalServerConfFromIni(source interface{}) (ServerConf, error) { func init() { fmt.Println("🐔 Initializing the plugin...") + // Get the frps.ini file path from the environment variable + frpsIniPath := os.Getenv("FRPS_INI_PATH") + + // If the environment variable is empty or not set + if frpsIniPath == "" { + // Use the default directory + frpsIniPath = "./frps.ini" + } + // Check if frps.ini exists - if _, err := os.Stat("./frps.ini"); os.IsNotExist(err) { - panic("frps.ini does not exist; move the frp-port-keeper binary to the same folder where the frps.ini located and call frp-port-keeper from there.") + if _, err := os.Stat(frpsIniPath); os.IsNotExist(err) { + panicMsg, _ := fmt.Printf("frps.ini does not exist at path %s; move the frp-port-keeper binary to the same folder where the frps.ini located and call frp-port-keeper from there.", frpsIniPath) + panic(panicMsg) } - var commonSection, err = UnmarshalServerConfFromIni("./frps.ini") + var commonSection, err = UnmarshalServerConfFromIni(frpsIniPath) if err != nil { fmt.Println("got error: ", err) } diff --git a/systemd/frp-port-keeper.service b/systemd/frp-port-keeper.service new file mode 100644 index 0000000..16f896e --- /dev/null +++ b/systemd/frp-port-keeper.service @@ -0,0 +1,16 @@ +[Unit] +Description=frp-port-keeper service +Documentation=https://github.com/librepod/frp-port-keeper +After=network.target nss-lookup.target + +[Service] +User=root +Group=root +WorkingDirectory=/etc/frp +Environment="FRPS_INI_PATH=/etc/frp/frps.ini" +ExecStart=/etc/frp/frp-port-keeper +Restart=on-failure +RestartPreventExitStatus=23 + +[Install] +WantedBy=multi-user.target diff --git a/systemd/frp-port-keeper@.service b/systemd/frp-port-keeper@.service new file mode 100644 index 0000000..16f896e --- /dev/null +++ b/systemd/frp-port-keeper@.service @@ -0,0 +1,16 @@ +[Unit] +Description=frp-port-keeper service +Documentation=https://github.com/librepod/frp-port-keeper +After=network.target nss-lookup.target + +[Service] +User=root +Group=root +WorkingDirectory=/etc/frp +Environment="FRPS_INI_PATH=/etc/frp/frps.ini" +ExecStart=/etc/frp/frp-port-keeper +Restart=on-failure +RestartPreventExitStatus=23 + +[Install] +WantedBy=multi-user.target