Skip to content

Commit 4aed381

Browse files
committed
Add ability to do https requests over unix socket
1 parent f64a39e commit 4aed381

File tree

2 files changed

+51
-7
lines changed

2 files changed

+51
-7
lines changed

server_examples/https.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package main
2+
3+
import (
4+
"crypto/tls"
5+
"fmt"
6+
"log"
7+
"net"
8+
"net/http"
9+
"os"
10+
)
11+
12+
func main() {
13+
if len(os.Args) != 4 {
14+
fmt.Println("Usage: %s <socket path> <cert> <key>", os.Args[0])
15+
os.Exit(-1)
16+
}
17+
listener, err := net.ListenUnix("unix", &net.UnixAddr{Name: os.Args[1]})
18+
if err != nil {
19+
panic(err)
20+
}
21+
config := &tls.Config{}
22+
config.Certificates = make([]tls.Certificate, 1)
23+
config.Certificates[0], err = tls.LoadX509KeyPair(os.Args[2], os.Args[3])
24+
if err != nil {
25+
panic(err)
26+
}
27+
tlsListener := tls.NewListener(listener, config)
28+
29+
log.Println(http.Serve(tlsListener, nil))
30+
}

unixhttp.go

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"crypto/tls"
45
"flag"
56
"fmt"
67
"io"
@@ -14,8 +15,8 @@ import (
1415
)
1516

1617
var (
17-
method, data, Cookie, Header string
18-
Verbose bool
18+
method, data, Cookie, Header string
19+
Verbose, Https, UnsecureHttps bool
1920
)
2021

2122
func usage() {
@@ -29,6 +30,8 @@ func setupFlags() {
2930
flag.StringVar(&Header, "H", "", "Additional headers: k1:v1|k2:v2|...")
3031
flag.StringVar(&Cookie, "b", "", "Add cookies: k1=v1|k2=v2|...") // b because thats what curl is
3132
flag.BoolVar(&Verbose, "v", false, "Verbose information")
33+
flag.BoolVar(&Https, "https", false, "Make an HTTPS request")
34+
flag.BoolVar(&UnsecureHttps, "k", false, "Accept any certificate")
3235
flag.Parse()
3336
}
3437

@@ -71,17 +74,17 @@ func main() {
7174
method = "POST"
7275
}
7376
// If data begins with @, it references a file
74-
if (string(data[0]) == "@" && len(data) > 1) {
75-
if (string(data[1:]) == "-") {
77+
if string(data[0]) == "@" && len(data) > 1 {
78+
if string(data[1:]) == "-" {
7679
buf, err := ioutil.ReadAll(os.Stdin)
77-
if (err != nil) {
80+
if err != nil {
7881
fmt.Println("Failed to read from stdin:", err)
7982
os.Exit(1)
8083
}
8184
reader = strings.NewReader(string(buf))
8285
} else {
8386
buf, err := ioutil.ReadFile(string(data[1:]))
84-
if (err != nil) {
87+
if err != nil {
8588
fmt.Println("Failed to open file:", err)
8689
os.Exit(1)
8790
}
@@ -108,11 +111,22 @@ func main() {
108111
os.Exit(1)
109112
}
110113

111-
conn, err := net.Dial("unix", u.Host)
114+
var conn net.Conn
115+
if Https {
116+
config := &tls.Config{}
117+
if UnsecureHttps {
118+
config.InsecureSkipVerify = true
119+
}
120+
conn, err = tls.Dial("unix", u.Host, config)
121+
} else {
122+
conn, err = net.Dial("unix", u.Host)
123+
}
124+
112125
if err != nil {
113126
fmt.Println("Fail to connect to", u.Host, ":", err)
114127
os.Exit(1)
115128
}
129+
116130
client := httputil.NewClientConn(conn, nil)
117131
res, err := requestExecute(conn, client, req)
118132
if err != nil {

0 commit comments

Comments
 (0)