Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add socket activation. #46

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ Options:
--port=2489 TCP port number
--line-ending Convert Line Ending(CR/CRLF)
--allow="0.0.0.0/0,::/0" Allow IP Range [Server only]
--socket Socket activation [Server only]
--host="localhost" Destination hostname [Client only]
--no-fallback-messages Do not show fallback messages [Client only]
--trans-loopback=true Translate loopback address [open subcommand only]
Expand Down Expand Up @@ -203,6 +204,35 @@ See:
- [WOW! and security? · Issue #14 · lemonade-command/lemonade](https://github.com/lemonade-command/lemonade/issues/14#)


### Socket activation

Lemonade can be activated with systemd by socket.

Create the following files in `~/.config/systemd/user`.

```sh
$ cat lemonade.socket
[Socket]
ListenStream=127.0.0.1:2489
Accept=no

[Install]
WantedBy=sockets.target
```

```sh
$ cat lemonade.service
[Unit]
Description=remote utility tool.
After=graphical-session.target

[Service]
ExecStart=/usr/bin/lemonade server -socket -allow 127.0.0.1
```

Enable the socket with `systemctl --user daemon-reload && systemctl --user enable --now lemonade.socket`.

Now whenever a request is made on `localhost:2489` the lemonade server will start automatically.

Links
-------
Expand Down
1 change: 1 addition & 0 deletions lemon/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type CLI struct {
// options
Port int
Allow string
Socket bool
Host string
TransLoopback bool
TransLocalfile bool
Expand Down
1 change: 1 addition & 0 deletions lemon/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ func (c *CLI) flags() *flag.FlagSet {
flags := flag.NewFlagSet("lemonade", flag.ContinueOnError)
flags.IntVar(&c.Port, "port", 2489, "TCP port number")
flags.StringVar(&c.Allow, "allow", "0.0.0.0/0,::/0", "Allow IP range")
flags.BoolVar(&c.Socket, "socket", false, "Use socket activation")
flags.StringVar(&c.Host, "host", "localhost", "Destination host name.")
flags.BoolVar(&c.Help, "help", false, "Show this message")
flags.BoolVar(&c.TransLoopback, "trans-loopback", true, "Translate loopback address")
Expand Down
38 changes: 37 additions & 1 deletion lemon/flag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"os"
"reflect"
"testing"
"time"
)

func TestCLIParse(t *testing.T) {
Expand All @@ -21,183 +22,218 @@ func TestCLIParse(t *testing.T) {
defaultHost := "localhost"
defaultAllow := "0.0.0.0/0,::/0"
defaultLogLevel := 1
defaultTimeout := time.Duration(1e8)

assert([]string{"xdg-open", "http://example.com"}, CLI{
Type: OPEN,
Host: defaultHost,
Port: defaultPort,
Allow: defaultAllow,
Socket: false,
DataSource: "http://example.com",
TransLoopback: true,
TransLocalfile: true,
LogLevel: defaultLogLevel,
Timeout: defaultTimeout,
})

assert([]string{"/usr/bin/xdg-open", "http://example.com"}, CLI{
Type: OPEN,
Host: defaultHost,
Port: defaultPort,
Allow: defaultAllow,
Socket: false,
DataSource: "http://example.com",
TransLoopback: true,
TransLocalfile: true,
LogLevel: defaultLogLevel,
Timeout: defaultTimeout,
})

assert([]string{"xdg-open"}, CLI{
Type: OPEN,
Host: defaultHost,
Port: defaultPort,
Allow: defaultAllow,
Socket: false,
TransLoopback: true,
TransLocalfile: true,
LogLevel: defaultLogLevel,
Timeout: defaultTimeout,
})

assert([]string{"pbpaste", "--port", "1124"}, CLI{
Type: PASTE,
Host: defaultHost,
Port: 1124,
Allow: defaultAllow,
Socket: false,
TransLoopback: true,
TransLocalfile: true,
LogLevel: defaultLogLevel,
Timeout: defaultTimeout,
})

assert([]string{"/usr/bin/pbpaste", "--port", "1124"}, CLI{
Type: PASTE,
Host: defaultHost,
Port: 1124,
Allow: defaultAllow,
Socket: false,
TransLoopback: true,
TransLocalfile: true,
LogLevel: defaultLogLevel,
Timeout: defaultTimeout,
})

assert([]string{"pbcopy", "hogefuga"}, CLI{
Type: COPY,
Host: defaultHost,
Port: defaultPort,
Allow: defaultAllow,
Socket: false,
DataSource: "hogefuga",
TransLoopback: true,
TransLocalfile: true,
LogLevel: defaultLogLevel,
Timeout: defaultTimeout,
})

assert([]string{"/usr/bin/pbcopy", "hogefuga"}, CLI{
Type: COPY,
Host: defaultHost,
Port: defaultPort,
Allow: defaultAllow,
Socket: false,
DataSource: "hogefuga",
TransLoopback: true,
TransLocalfile: true,
LogLevel: defaultLogLevel,
Timeout: defaultTimeout,
})

assert([]string{"lemonade", "--host", "192.168.0.1", "--port", "1124", "open", "http://example.com"}, CLI{
Type: OPEN,
Host: "192.168.0.1",
Port: 1124,
Allow: defaultAllow,
Socket: false,
DataSource: "http://example.com",
TransLoopback: true,
TransLocalfile: true,
LogLevel: defaultLogLevel,
Timeout: defaultTimeout,
})

assert([]string{"lemonade", "copy", "hogefuga"}, CLI{
Type: COPY,
Host: defaultHost,
Port: defaultPort,
Allow: defaultAllow,
Socket: false,
DataSource: "hogefuga",
TransLoopback: true,
TransLocalfile: true,
LogLevel: defaultLogLevel,
Timeout: defaultTimeout,
})

assert([]string{"lemonade", "paste"}, CLI{
Type: PASTE,
Host: defaultHost,
Port: defaultPort,
Allow: defaultAllow,
Socket: false,
TransLoopback: true,
TransLocalfile: true,
LogLevel: defaultLogLevel,
Timeout: defaultTimeout,
})

assert([]string{"lemonade", "--allow", "192.168.0.0/24", "server", "--port", "1124"}, CLI{
assert([]string{"lemonade", "--allow", "192.168.0.0/24", "server", "--port", "1124", "--socket"}, CLI{
Type: SERVER,
Host: defaultHost,
Port: 1124,
Allow: "192.168.0.0/24",
Socket: true,
TransLoopback: true,
TransLocalfile: true,
LogLevel: defaultLogLevel,
Timeout: defaultTimeout,
})

assert([]string{"lemonade", "open", "--trans-loopback=false"}, CLI{
Type: OPEN,
Host: defaultHost,
Port: defaultPort,
Allow: defaultAllow,
Socket: false,
TransLoopback: false,
TransLocalfile: true,
LogLevel: defaultLogLevel,
Timeout: defaultTimeout,
})

assert([]string{"lemonade", "open", "--trans-loopback=true"}, CLI{
Type: OPEN,
Host: defaultHost,
Port: defaultPort,
Allow: defaultAllow,
Socket: false,
TransLoopback: true,
TransLocalfile: true,
LogLevel: defaultLogLevel,
Timeout: defaultTimeout,
})

assert([]string{"lemonade", "open", "--trans-localfile=false"}, CLI{
Type: OPEN,
Host: defaultHost,
Port: defaultPort,
Allow: defaultAllow,
Socket: false,
TransLoopback: true,
TransLocalfile: false,
LogLevel: defaultLogLevel,
Timeout: defaultTimeout,
})

assert([]string{"lemonade", "open", "--trans-localfile=true"}, CLI{
Type: OPEN,
Host: defaultHost,
Port: defaultPort,
Allow: defaultAllow,
Socket: false,
TransLoopback: true,
TransLocalfile: true,
LogLevel: defaultLogLevel,
Timeout: defaultTimeout,
})

assert([]string{"lemonade", "copy", "--no-fallback-messages", "hogefuga"}, CLI{
Type: COPY,
Host: defaultHost,
Port: defaultPort,
Allow: defaultAllow,
Socket: false,
DataSource: "hogefuga",
TransLoopback: true,
TransLocalfile: true,
NoFallbackMessages: true,
LogLevel: defaultLogLevel,
Timeout: defaultTimeout,
})

assert([]string{"lemonade", "paste", "--no-fallback-messages"}, CLI{
Type: PASTE,
Host: defaultHost,
Port: defaultPort,
Allow: defaultAllow,
Socket: false,
TransLoopback: true,
TransLocalfile: true,
NoFallbackMessages: true,
LogLevel: defaultLogLevel,
Timeout: defaultTimeout,
})
}
1 change: 1 addition & 0 deletions lemon/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Options:
--port=2489 TCP port number
--line-ending Convert Line Ending (CR/CRLF)
--allow="0.0.0.0/0,::/0" Allow IP Range [Server only]
--socket Socket activation [Server only]
--host="localhost" Destination hostname [Client only]
--no-fallback-messages Do not show fallback messages [Client only]
--trans-loopback=true Translate loopback address [open subcommand only]
Expand Down
25 changes: 18 additions & 7 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"net"
"net/rpc"
"os"

log "github.com/inconshreveable/log15"

Expand All @@ -24,13 +25,23 @@ func Serve(c *lemon.CLI, logger log.Logger) error {
return err
}

addr, err := net.ResolveTCPAddr("tcp", fmt.Sprintf(":%d", port))
if err != nil {
return err
}
l, err := net.ListenTCP("tcp", addr)
if err != nil {
return err
var l net.Listener

if c.Socket {
listenerFile := os.NewFile(3, "listener")
l, err = net.FileListener(listenerFile)
if err != nil {
return err
}
} else {
addr, err := net.ResolveTCPAddr("tcp", fmt.Sprintf(":%d", port))
if err != nil {
return err
}
l, err = net.ListenTCP("tcp", addr)
if err != nil {
return err
}
}

for {
Expand Down