Skip to content

Commit c9e62e3

Browse files
authored
Merge pull request #65 from davrodpin/windows_support
Add support for Windows
2 parents 6df3fc7 + 1238eb1 commit c9e62e3

File tree

8 files changed

+59
-10
lines changed

8 files changed

+59
-10
lines changed

.gitignore

+6
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,9 @@
33

44
/test-env/key
55
/test-env/key.pub
6+
7+
/tunnel/testdata/.ssh
8+
9+
*.swp
10+
*~
11+
*.un~

Makefile

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
.PHONY: test cover install bin test-env rm-test-env
2+
13
LDFLAGS := -X main.version=$(version)
24

35
test:
@@ -19,6 +21,8 @@ endif
1921
cd bin && tar c mole | gzip > mole$(version).darwin-amd64.tar.gz && rm mole && cd -
2022
GOOS=linux GOARCH=amd64 go build -o bin/mole -ldflags "$(LDFLAGS)" github.com/davrodpin/mole/cmd/mole
2123
cd bin && tar c mole | gzip > mole$(version).linux-amd64.tar.gz && rm mole && cd -
24+
GOOS=windows GOARCH=amd64 go build -o bin/mole.exe -ldflags "$(LDFLAGS)" github.com/davrodpin/mole/cmd/mole
25+
cd bin && tar c mole.exe | gzip > mole$(version).windows-amd64.tar.gz && rm mole.exe && cd -
2226

2327
add-network:
2428
-@docker network create --subnet=192.168.33.0/24 mole

cmd/mole/main.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,16 @@ func main() {
3333
app.PrintUsage()
3434
os.Exit(1)
3535
}
36+
3637
log.SetOutput(os.Stdout)
3738

38-
instancesDir = fmt.Sprintf("%s/.mole/instances", os.Getenv("HOME"))
39+
home, err := os.UserHomeDir()
40+
if err != nil {
41+
log.Errorf("error starting mole: %v", err)
42+
os.Exit(1)
43+
}
44+
45+
instancesDir = fmt.Sprintf("%s/.mole/instances", home)
3946

4047
err = createInstancesDir()
4148
if err != nil {

storage/storage.go

+20-5
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,12 @@ func Remove(name string) (*Tunnel, error) {
9797
func loadStore() (*Store, error) {
9898
var store *Store
9999

100-
if _, err := os.Stat(storePath()); err != nil {
100+
sp, err := storePath()
101+
if err != nil {
102+
return nil, err
103+
}
104+
105+
if _, err := os.Stat(sp); err != nil {
101106
store = &Store{Tunnels: make(map[string]*Tunnel)}
102107
store, err = createStore(store)
103108
if err != nil {
@@ -107,15 +112,20 @@ func loadStore() (*Store, error) {
107112
return store, nil
108113
}
109114

110-
if _, err := toml.DecodeFile(storePath(), &store); err != nil {
115+
if _, err := toml.DecodeFile(sp, &store); err != nil {
111116
return nil, err
112117
}
113118

114119
return store, nil
115120
}
116121

117122
func createStore(store *Store) (*Store, error) {
118-
f, err := os.Create(storePath())
123+
sp, err := storePath()
124+
if err != nil {
125+
return nil, err
126+
}
127+
128+
f, err := os.Create(sp)
119129
if err != nil {
120130
return nil, err
121131
}
@@ -131,6 +141,11 @@ func createStore(store *Store) (*Store, error) {
131141
return store, nil
132142
}
133143

134-
func storePath() string {
135-
return filepath.Join(os.Getenv("HOME"), ".mole.conf")
144+
func storePath() (string, error) {
145+
home, err := os.UserHomeDir()
146+
if err != nil {
147+
return "", err
148+
}
149+
150+
return filepath.Join(home, ".mole.conf"), nil
136151
}

storage/storage_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ func TestMain(m *testing.M) {
102102
defer os.RemoveAll(dir)
103103

104104
os.Setenv("HOME", dir)
105+
os.Setenv("USERPROFILE", dir)
105106

106107
code := m.Run()
107108

tunnel/config.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,13 @@ type SSHConfigFile struct {
1919
// NewSSHConfigFile creates a new instance of SSHConfigFile based on the
2020
// ssh config file from $HOME/.ssh/config.
2121
func NewSSHConfigFile() (*SSHConfigFile, error) {
22-
configPath := filepath.Join(os.Getenv("HOME"), ".ssh", "config")
22+
home, err := os.UserHomeDir()
23+
if err != nil {
24+
return nil, err
25+
}
26+
27+
configPath := filepath.Join(home, ".ssh", "config")
28+
2329
f, err := os.Open(filepath.Clean(configPath))
2430
if err != nil {
2531
return nil, err

tunnel/tunnel.go

+12-2
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,12 @@ func NewServer(user, address, key string) (*Server, error) {
7272
}
7373

7474
if key == "" {
75-
key = filepath.Join(os.Getenv("HOME"), ".ssh", "id_rsa")
75+
home, err := os.UserHomeDir()
76+
if err != nil {
77+
return nil, fmt.Errorf("could not obtain user home directory: %v", err)
78+
}
79+
80+
key = filepath.Join(home, ".ssh", "id_rsa")
7681
}
7782

7883
pk, err := NewPemKey(key, "")
@@ -285,7 +290,12 @@ func knownHostsCallback(insecure bool) (ssh.HostKeyCallback, error) {
285290
}
286291
} else {
287292
var err error
288-
knownHostFile := filepath.Join(os.Getenv("HOME"), ".ssh", "known_hosts")
293+
home, err := os.UserHomeDir()
294+
if err != nil {
295+
return nil, fmt.Errorf("could not obtain user home directory :%v", err)
296+
}
297+
298+
knownHostFile := filepath.Join(home, ".ssh", "known_hosts")
289299
log.Debugf("known_hosts file used: %s", knownHostFile)
290300

291301
clb, err = knownhosts.New(knownHostFile)

tunnel/tunnel_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,6 @@ func TestServerOptions(t *testing.T) {
108108
t.Errorf("unexpected result : expected: %s, result: %s", test.expected, s)
109109
}
110110
}
111-
112111
}
113112

114113
func TestTunnelOptions(t *testing.T) {
@@ -343,6 +342,7 @@ func prepareTestEnv() {
343342
}
344343

345344
os.Setenv("HOME", home)
345+
os.Setenv("USERPROFILE", home)
346346
}
347347

348348
// get performs a http request using the given client appending the given

0 commit comments

Comments
 (0)