Skip to content

Commit

Permalink
Pass frps.ini file via an env var (#5)
Browse files Browse the repository at this point in the history
* Introduce FRPS_INI_PATH env var

* Add sample systemd unit files
  • Loading branch information
cyxou authored Jun 11, 2023
1 parent 9c15001 commit cf235ac
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 5 deletions.
1 change: 1 addition & 0 deletions Earthfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
16 changes: 13 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
16 changes: 16 additions & 0 deletions systemd/frp-port-keeper.service
Original file line number Diff line number Diff line change
@@ -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
16 changes: 16 additions & 0 deletions systemd/[email protected]
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit cf235ac

Please sign in to comment.