Add Unix domain socket support for --listen
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Close #4541
authored by Junegunn Choi · committed by Junegunn Choi · parent c38c6ca · Browse files · 5 files·+86 added−25 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+86 added−25 removed
- other:+86 added−25 removed
Provenance
- Commit
01cb38a- Tree
4f65383- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
c38c6ca
5 files changed+86 added−25 removed
modified:man/man1/fzf.1+27 added-7 removed
1131 unmodified lines1132 On Windows, the default value is \fBcmd /s/c\fR when \fB$SHELL\fR is not1133 set.1134 1135 .TP1136-.B "\-\-listen[=[ADDR:]PORT]" "\-\-listen\-unsafe[=[ADDR:]PORT]"1137-Start HTTP server and listen on the given address. It allows external processes1138-to send actions to perform via POST method.1136+.B "\-\-listen[=SOCKET_PATH|[ADDR:]PORT]" "\-\-listen\-unsafe[=[ADDR:]PORT]"1137+Start HTTP server and listen on the given address or Unix socket. It allows1138+external processes to send actions to perform via POST method and query the1139+program state via GET method. For the argument to be recognized as a socket1140+path, it must have \fB.sock\fR extension.1141 1142 - If the port number is omitted or given as 0, fzf will automatically choose1141-a port and export it as \fBFZF_PORT\fR environment variable to the child processes1143+a port and export it as \fBFZF_PORT\fR environment variable to the child processes.1144 1145+- If a Unix socket path is given, fzf will create a Unix domain socket at the1146+ given path. The existing file will be removed. The path to the socket file1147+ is exported as \fBFZF_SOCK\fR environment variable.1148+1149 - If \fBFZF_API_KEY\fR environment variable is set, the server would require1144-sending an API key with the same value in the \fBx\-api\-key\fR HTTP header1150+ sending an API key with the same value in the \fBx\-api\-key\fR HTTP header.1151 1146-- \fBFZF_API_KEY\fR is required for a non-localhost listen address1152+- \fBFZF_API_KEY\fR is required for a non-localhost listen address.1153 1148-- To allow remote process execution, use \fB\-\-listen\-unsafe\fR1154+- To allow remote process execution, use \fB\-\-listen\-unsafe\fR.1155 1156 e.g.1157 \fB# Start HTTP server on port 62661158 fzf \-\-listen 626630 unmodified lines1189 echo +first1190 '1191 \fR1192 1193+Here is an example script that uses a Unix socket instead of a TCP port.1194+1195+ \fB1196+ fzf --listen=/tmp/fzf.sock1197+1198+ # GET1199+ curl --unix-socket /tmp/fzf.sock http1200+1201+ # POST1202+ curl --unix-socket /tmp/fzf.sock http -d up1203+ \fR1204+1205 .SS DIRECTORY TRAVERSAL1206 .TP1207 .B "\-\-walker=[file][,dir][,follow][,hidden]"1208 Determines the behavior of the built-in directory walker that is used when181 unmodified lines1390 .BR FZF_KEY " The name of the last key pressed"1391 .br1392 .BR FZF_PORT " Port number when \-\-listen option is used"1393 .br1394+.BR FZF_SOCK " Unix socket path when \-\-listen option is used"1395+.br1396 .BR FZF_PREVIEW_TOP " Top position of the preview window"1397 .br1398 .BR FZF_PREVIEW_LEFT " Left position of the preview window"1399 .br776 unmodified linesmodified:src/options.go+3 added-1 removed
204 unmodified lines205 --bind=BINDINGS Custom key/event bindings206 207 ADVANCED208 --with-shell=STR Shell command and flags to start child processes with209+ --listen[=SOCKET_PATH] Start HTTP server to receive actions via Unix domain socket210+ (Path should end with .sock)209- --listen[=[ADDR:]PORT] Start HTTP server to receive actions (POST /)211+ --listen[=[ADDR:]PORT] Start HTTP server to receive actions via TCP212 (To allow remote process execution, use --listen-unsafe)213 214 DIRECTORY TRAVERSAL (Only used when $FZF_DEFAULT_COMMAND is not set)215 --walker=OPTS [file][,dir][,follow][,hidden] (default: file,follow,hidden)3567 unmodified linesmodified:src/server.go+32 added-16 removed
44 unmodified lines45 46 type listenAddress struct {47 host string48 port int49+ sock string50 }51 52 func (addr listenAddress) IsLocal() bool {52- return addr.host == "localhost" || addr.host == "127.0.0.1"53+ return addr.host == "localhost" || addr.host == "127.0.0.1" || len(addr.sock) > 054 }55 55-var defaultListenAddr = listenAddress{"localhost", 0}56+var defaultListenAddr = listenAddress{"localhost", 0, ""}57 58 func parseListenAddress(address string) (listenAddress, error) {59+ if strings.HasSuffix(address, ".sock") {60+ return listenAddress{"", 0, address}, nil61+ }62+63 parts := strings.SplitN(address, ":", 3)64 if len(parts) == 1 {65 parts = []string{"localhost", parts[0]}66 }7 unmodified lines74 }75 if len(parts[0]) == 0 {76 parts[0] = "localhost"77 }73- return listenAddress{parts[0], port}, nil78+ return listenAddress{parts[0], port, ""}, nil79 }80 81 func startHttpServer(address listenAddress, actionChannel chan []*action, getHandler func(getParams) string) (net.Listener, int, error) {82 host := address.host1 unmodified line84 apiKey := os.Getenv("FZF_API_KEY")85 if !address.IsLocal() && len(apiKey) == 0 {86 return nil, port, errors.New("FZF_API_KEY is required to allow remote access")87 }83- addrStr := fmt.Sprintf("%s:%d", host, port)84- listener, err := net.Listen("tcp", addrStr)85- if err != nil {86- return nil, port, fmt.Errorf("failed to listen on %s", addrStr)87- }88- if port == 0 {89- addr := listener.Addr().String()90- parts := strings.Split(addr, ":")91- if len(parts) < 2 {92- return nil, port, fmt.Errorf("cannot extract port: %s", addr)88+89+ var listener net.Listener90+ var err error91+ if len(address.sock) > 0 {92+ os.Remove(address.sock)93+ listener, err = net.Listen("unix", address.sock)94+ if err != nil {95+ return nil, 0, fmt.Errorf("failed to listen on %s", address.sock)96 }94- var err error95- port, err = strconv.Atoi(parts[len(parts)-1])97+ } else {98+ addrStr := fmt.Sprintf("%s:%d", host, port)99+ listener, err = net.Listen("tcp", addrStr)100 if err != nil {97- return nil, port, err101+ return nil, port, fmt.Errorf("failed to listen on %s", addrStr)102 }103+ if port == 0 {104+ addr := listener.Addr().String()105+ parts := strings.Split(addr, ":")106+ if len(parts) < 2 {107+ return nil, port, fmt.Errorf("cannot extract port: %s", addr)108+ }109+ var err error110+ port, err = strconv.Atoi(parts[len(parts)-1])111+ if err != nil {112+ return nil, port, err113+ }114+ }115 }116 117 server := httpServer{118 apiKey: []byte(apiKey),144 unmodified linesmodified:src/terminal.go+6 added-1 removed
1266 unmodified lines1267 if err != nil {1268 return nil, err1269 }1270 t.listener = listener1271+ if port > 0 {1271- t.listenPort = &port1272+ t.listenPort = &port1273+ }1274 }1275 1276 if t.hasStartActions {1277 t.eventChan <- tui.Start.AsEvent()15 unmodified lines1293 }1294 1295 func (t *Terminal) environImpl(forPreview bool) []string {1296 env := os.Environ()1297+ if t.listenAddr != nil && len(t.listenAddr.sock) > 0 {1298+ env = append(env, "FZF_SOCK="+t.listenAddr.sock)1299+ }1300 if t.listenPort != nil {1301 env = append(env, fmt.Sprintf("FZF_PORT=%d", *t.listenPort))1302 }1303 env = append(env, "FZF_QUERY="+string(t.input))5965 unmodified linesmodified:CHANGELOG.md+18 added
122 unmodified lines123 - The gutter column is now narrower, rendered with the left-half block character (`▌`).124 - Markers no longer use background colors.125 - The `--color base16` theme (alias: `16`) has been updated for better compatibility with both dark and light themes.126 127+### `--listen` now supports Unix domain sockets128+129+If an argument to `--listen` ends with `.sock`, fzf will listen on a Unix130+domain socket at the specified path.131+132+```sh133+fzf --listen /tmp/fzf.sock --no-tmux134+135+# GET136+curl --unix-socket /tmp/fzf.sock http137+138+# POST139+curl --unix-socket /tmp/fzf.sock http -d up140+```141+142+Note that any existing file at the given path will be removed before creating143+the socket, so avoid using an important file path.144+145 ### Added options146 147 #### `--gutter CHAR`148 2882 unmodified lines