Skip to content

Commit f4242d8

Browse files
authored
feat(api): add hostEndpoint in /info (#103)
Signed-off-by: Kevin Cui <bh@bugs.cc>
1 parent 45eb0f4 commit f4242d8

File tree

3 files changed

+84
-11
lines changed

3 files changed

+84
-11
lines changed

pkg/ipc/restful/run.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,23 @@ func (r *routerRun) mux() http.Handler {
7575
}
7676

7777
type infoResponse struct {
78-
PodmanHost string `json:"podmanHost"`
79-
PodmanPort int `json:"podmanPort"`
78+
PodmanHost string `json:"podmanHost"`
79+
PodmanPort int `json:"podmanPort"`
80+
HostEndpoint string `json:"hostEndpoint"`
8081
}
8182

8283
func (r *routerRun) info(w http.ResponseWriter, req *http.Request) {
84+
he, err := wsl.HostEndpoint(r.log, r.opt.DistroName)
85+
if err != nil {
86+
r.log.Warnf("Failed to get host endpoint: %v", err)
87+
http.Error(w, "failed to get host endpoint", http.StatusInternalServerError)
88+
return
89+
}
90+
8391
_ = json.NewEncoder(w).Encode(&infoResponse{
84-
PodmanHost: "127.0.0.1",
85-
PodmanPort: r.opt.PodmanPort,
92+
PodmanHost: "127.0.0.1",
93+
PodmanPort: r.opt.PodmanPort,
94+
HostEndpoint: he,
8695
})
8796
}
8897

pkg/wsl/config.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ func NewConfig(log *logger.Context) *Config {
2525
}
2626

2727
func (c *Config) ExistIncompatible() bool {
28-
return c.findKey("wsl2", "kernel")
28+
_, ok := c.GetValue("wsl2", "kernel")
29+
return ok
2930
}
3031

3132
func (c *Config) Fix() error {
@@ -56,17 +57,17 @@ func (c *Config) Open() error {
5657
return nil
5758
}
5859

59-
func (c *Config) findKey(expectSection string, expectKey string) bool {
60+
func (c *Config) GetValue(expectSection string, expectKey string) (string, bool) {
6061
wslConfigPath, ok := c.path()
6162
if !ok {
6263
c.log.Info("WSL config file not found")
63-
return false
64+
return "", false
6465
}
6566

6667
file, err := os.Open(wslConfigPath)
6768
if err != nil {
6869
c.log.Warnf("Failed to open .wslconfig file: %v", err)
69-
return false
70+
return "", false
7071
}
7172
defer file.Close()
7273

@@ -107,16 +108,16 @@ func (c *Config) findKey(expectSection string, expectKey string) bool {
107108
}
108109

109110
c.log.Infof("Find %s key in .wslconfig: %s", expectKey, line)
110-
return true
111+
return val, true
111112
}
112113

113114
if err := scanner.Err(); err != nil {
114115
c.log.Warnf("Failed to scan .wslconfig: %v", err)
115-
return false
116+
return "", false
116117
}
117118

118119
c.log.Infof("No %s key config found in WSL config file: %s", expectKey, wslConfigPath)
119-
return false
120+
return "", false
120121
}
121122

122123
func (c *Config) commentKey(expectKey string) error {

pkg/wsl/host.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// SPDX-FileCopyrightText: 2025 OOMOL, Inc. <https://www.oomol.com>
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package wsl
5+
6+
import (
7+
"bytes"
8+
"fmt"
9+
"strings"
10+
11+
"github.com/oomol-lab/ovm-win/pkg/logger"
12+
"github.com/oomol-lab/ovm-win/pkg/util"
13+
)
14+
15+
var (
16+
_hostEndpoint string
17+
)
18+
19+
func HostEndpoint(log *logger.Context, name string) (string, error) {
20+
// TODO(@BlackHole1): getHostEndpoint may be slow, so it needs to be thread-safe here
21+
if _hostEndpoint != "" {
22+
return _hostEndpoint, nil
23+
}
24+
25+
if he, err := getHostEndpoint(log, name); err != nil {
26+
return "", fmt.Errorf("failed to get host endpoint: %w", err)
27+
} else {
28+
_hostEndpoint = he
29+
}
30+
31+
log.Infof("Host endpoint is: %s", _hostEndpoint)
32+
33+
return _hostEndpoint, nil
34+
}
35+
36+
func isMirroredNetwork(log *logger.Context) bool {
37+
val, _ := NewConfig(log).GetValue("wsl2", "networkingMode")
38+
return val == "mirrored"
39+
}
40+
41+
func getHostEndpoint(log *logger.Context, name string) (string, error) {
42+
if isMirroredNetwork(log) {
43+
return "localhost", nil
44+
}
45+
46+
// TODO(@BlackHole1): improve wslInvoke
47+
newArgs := []string{"-d", name, "/bin/sh", "-c", "ip route | grep '^default' | awk '{print $3}'"}
48+
cmd := util.SilentCmd(Find(), newArgs...)
49+
var stdout bytes.Buffer
50+
var stderr bytes.Buffer
51+
cmd.Stdout = &stdout
52+
cmd.Stderr = &stderr
53+
cmd.Env = []string{"WSL_UTF8=1"}
54+
55+
cmdStr := fmt.Sprintf("%s %s", Find(), strings.Join(newArgs, " "))
56+
57+
log.Infof("Running command in distro: %s", cmdStr)
58+
if err := cmd.Run(); err != nil {
59+
return "", fmt.Errorf("failed to run command in distro: %w, %s", err, stderr.String())
60+
}
61+
62+
return strings.TrimSpace(stdout.String()), nil
63+
}

0 commit comments

Comments
 (0)