Skip to content
Myr

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

5 files+86 added−25 removed

modified:man/man1/fzf.1+27 added-7 removed
1131 unmodified lines
1132On Windows, the default value is \fBcmd /s/c\fR when \fB$SHELL\fR is not1133set.11341135.TP1136.B "\-\-listen[=[ADDR:]PORT]" "\-\-listen\-unsafe[=[ADDR:]PORT]"1137Start HTTP server and listen on the given address. It allows external processes1138to send actions to perform via POST method.1136.B "\-\-listen[=SOCKET_PATH|[ADDR:]PORT]" "\-\-listen\-unsafe[=[ADDR:]PORT]"1137Start HTTP server and listen on the given address or Unix socket. It allows1138external processes to send actions to perform via POST method and query the1139program state via GET method. For the argument to be recognized as a socket1140path, it must have \fB.sock\fR extension.11411142- If the port number is omitted or given as 0, fzf will automatically choose1141a port and export it as \fBFZF_PORT\fR environment variable to the child processes1143a port and export it as \fBFZF_PORT\fR environment variable to the child processes.11441145- 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.11481149- If \fBFZF_API_KEY\fR environment variable is set, the server would require1144sending 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.11511146- \fBFZF_API_KEY\fR is required for a non-localhost listen address1152- \fBFZF_API_KEY\fR is required for a non-localhost listen address.11531148- To allow remote process execution, use \fB\-\-listen\-unsafe\fR1154- To allow remote process execution, use \fB\-\-listen\-unsafe\fR.11551156e.g.1157 \fB# Start HTTP server on port 62661158 fzf \-\-listen 6266
30 unmodified lines
1189 echo +first1190 '1191 \fR11921193Here is an example script that uses a Unix socket instead of a TCP port.11941195 \fB1196 fzf --listen=/tmp/fzf.sock11971198 # GET1199 curl --unix-socket /tmp/fzf.sock http12001201 # POST1202 curl --unix-socket /tmp/fzf.sock http -d up1203 \fR12041205.SS DIRECTORY TRAVERSAL1206.TP1207.B "\-\-walker=[file][,dir][,follow][,hidden]"1208Determines the behavior of the built-in directory walker that is used when
181 unmodified lines
1390.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.br
776 unmodified lines
modified:src/options.go+3 added-1 removed
204 unmodified lines
205 --bind=BINDINGS Custom key/event bindings206207 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)213214 DIRECTORY TRAVERSAL (Only used when $FZF_DEFAULT_COMMAND is not set)215 --walker=OPTS [file][,dir][,follow][,hidden] (default: file,follow,hidden)
3567 unmodified lines
modified:src/server.go+32 added-16 removed
44 unmodified lines
4546type listenAddress struct {47 host string48 port int49 sock string50}5152func (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}5555var defaultListenAddr = listenAddress{"localhost", 0}56var defaultListenAddr = listenAddress{"localhost", 0, ""}5758func parseListenAddress(address string) (listenAddress, error) {59 if strings.HasSuffix(address, ".sock") {60 return listenAddress{"", 0, address}, nil61 }6263 parts := strings.SplitN(address, ":", 3)64 if len(parts) == 1 {65 parts = []string{"localhost", parts[0]}66 }
7 unmodified lines
74 }75 if len(parts[0]) == 0 {76 parts[0] = "localhost"77 }73 return listenAddress{parts[0], port}, nil78 return listenAddress{parts[0], port, ""}, nil79}8081func startHttpServer(address listenAddress, actionChannel chan []*action, getHandler func(getParams) string) (net.Listener, int, error) {82 host := address.host
1 unmodified line
84 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)8889 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 }116117 server := httpServer{118 apiKey: []byte(apiKey),
144 unmodified lines
modified:src/terminal.go+6 added-1 removed
1266 unmodified lines
1267 if err != nil {1268 return nil, err1269 }1270 t.listener = listener1271 if port > 0 {1271 t.listenPort = &port1272 t.listenPort = &port1273 }1274 }12751276 if t.hasStartActions {1277 t.eventChan <- tui.Start.AsEvent()
15 unmodified lines
1293}12941295func (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 lines
modified:CHANGELOG.md+18 added
122 unmodified lines
123- 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.126127### `--listen` now supports Unix domain sockets128129If an argument to `--listen` ends with `.sock`, fzf will listen on a Unix130domain socket at the specified path.131132```sh133fzf --listen /tmp/fzf.sock --no-tmux134135# GET136curl --unix-socket /tmp/fzf.sock http137138# POST139curl --unix-socket /tmp/fzf.sock http -d up140```141142Note that any existing file at the given path will be removed before creating143the socket, so avoid using an important file path.144145### Added options146147#### `--gutter CHAR`148
2882 unmodified lines