Skip to content

Commit a9ac2c6

Browse files
committed
Basic proxy interaction with downloader
Signed-off-by: Anders F Björklund <anders.f.bjorklund@gmail.com>
1 parent 864984e commit a9ac2c6

File tree

1 file changed

+46
-2
lines changed

1 file changed

+46
-2
lines changed

Diff for: pkg/hostagent/proxy/proxy.go

+46-2
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,19 @@ package proxy
44

55
import (
66
"bufio"
7+
"bytes"
78
"context"
9+
"io"
810
"net"
911
"net/http"
12+
"os"
13+
"path/filepath"
1014
"regexp"
1115
"strconv"
1216
"time"
1317

18+
"github.com/lima-vm/lima/pkg/downloader"
19+
1420
"github.com/elazarl/goproxy"
1521
"github.com/sirupsen/logrus"
1622
)
@@ -47,11 +53,51 @@ func Start(opts ServerOptions) (*Server, error) {
4753
return server, nil
4854
}
4955

56+
func sendFile(req *http.Request, path string) (*http.Response, error) {
57+
resp := &http.Response{}
58+
resp.Request = req
59+
resp.TransferEncoding = req.TransferEncoding
60+
resp.Header = make(http.Header)
61+
status := http.StatusOK
62+
resp.StatusCode = status
63+
resp.Status = http.StatusText(status)
64+
b, err := os.ReadFile(path)
65+
if err != nil {
66+
return nil, err
67+
}
68+
resp.Body = io.NopCloser(bytes.NewBuffer(b))
69+
resp.Header.Set("Content-Type", "application/octet-stream")
70+
//resp.Header.Set("Last-Modified", ")
71+
resp.ContentLength = int64(len(b))
72+
return resp, nil
73+
}
74+
5075
func listenAndServe(opts ServerOptions) (*http.Server, error) {
76+
ucd, err := os.UserCacheDir()
77+
if err != nil {
78+
return nil, err
79+
}
80+
cacheDir := filepath.Join(ucd, "lima")
81+
downloader.HideProgress = true
82+
5183
addr := net.JoinHostPort(opts.Address, strconv.Itoa(opts.TCPPort))
5284
proxy := goproxy.NewProxyHttpServer()
5385
proxy.OnRequest(goproxy.ReqHostMatches(regexp.MustCompile("^.*$"))).
5486
HandleConnect(goproxy.AlwaysMitm)
87+
proxy.OnRequest().DoFunc(func(req *http.Request, ctx *goproxy.ProxyCtx) (*http.Request, *http.Response) {
88+
url := req.URL.String()
89+
if res, err := downloader.Cached(url, downloader.WithCacheDir(cacheDir)); err == nil {
90+
if resp, err := sendFile(req, res.CachePath); err == nil {
91+
return nil, resp
92+
}
93+
}
94+
if res, err := downloader.Download(context.Background(), "", url, downloader.WithCacheDir(cacheDir)); err == nil {
95+
if resp, err := sendFile(req, res.CachePath); err == nil {
96+
return nil, resp
97+
}
98+
}
99+
return req, nil
100+
})
55101
proxy.OnRequest(goproxy.ReqHostMatches(regexp.MustCompile("^.*:80$"))).
56102
HijackConnect(func(req *http.Request, client net.Conn, ctx *goproxy.ProxyCtx) {
57103
defer func() {
@@ -61,8 +107,6 @@ func listenAndServe(opts ServerOptions) (*http.Server, error) {
61107
}
62108
client.Close()
63109
}()
64-
url := req.URL.String()
65-
ctx.Logf("URL: %s", url)
66110
clientBuf := bufio.NewReadWriter(bufio.NewReader(client), bufio.NewWriter(client))
67111
remote, err := net.Dial("tcp", req.URL.Host)
68112
if err != nil {

0 commit comments

Comments
 (0)