Skip to content

Commit

Permalink
Basic proxy interaction with downloader
Browse files Browse the repository at this point in the history
Signed-off-by: Anders F Björklund <[email protected]>
  • Loading branch information
afbjorklund committed Oct 18, 2024
1 parent b1a9cab commit 2ec809f
Showing 1 changed file with 51 additions and 2 deletions.
53 changes: 51 additions & 2 deletions pkg/hostagent/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@ package proxy

import (
"bufio"
"bytes"
"context"
"io"
"net"
"net/http"
"os"
"path/filepath"
"regexp"
"strconv"
"time"

"github.com/lima-vm/lima/pkg/downloader"

"github.com/elazarl/goproxy"
"github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -47,11 +53,56 @@ func Start(opts ServerOptions) (*Server, error) {
return server, nil
}

func sendFile(req *http.Request, path string, lastModified time.Time, contentType string) (*http.Response, error) {
resp := &http.Response{}
resp.Request = req
resp.TransferEncoding = req.TransferEncoding
resp.Header = make(http.Header)
status := http.StatusOK
resp.StatusCode = status
resp.Status = http.StatusText(status)
b, err := os.ReadFile(path)
if err != nil {
return nil, err
}
resp.Body = io.NopCloser(bytes.NewBuffer(b))
if contentType == "" {
contentType = "application/octet-stream"
}
resp.Header.Set("Content-Type", contentType)
if !lastModified.IsZero() {
resp.Header.Set("Last-Modified", lastModified.Format(http.TimeFormat))
}
resp.ContentLength = int64(len(b))
return resp, nil
}

func listenAndServe(opts ServerOptions) (*http.Server, error) {
ucd, err := os.UserCacheDir()
if err != nil {
return nil, err
}
cacheDir := filepath.Join(ucd, "lima")
downloader.HideProgress = true

addr := net.JoinHostPort(opts.Address, strconv.Itoa(opts.TCPPort))
proxy := goproxy.NewProxyHttpServer()
proxy.OnRequest(goproxy.ReqHostMatches(regexp.MustCompile("^.*$"))).
HandleConnect(goproxy.AlwaysMitm)
proxy.OnRequest().DoFunc(func(req *http.Request, _ *goproxy.ProxyCtx) (*http.Request, *http.Response) {
url := req.URL.String()
if res, err := downloader.Cached(url, downloader.WithCacheDir(cacheDir)); err == nil {
if resp, err := sendFile(req, res.CachePath, res.LastModified, res.ContentType); err == nil {
return nil, resp
}
}
if res, err := downloader.Download(context.Background(), "", url, downloader.WithCacheDir(cacheDir)); err == nil {
if resp, err := sendFile(req, res.CachePath, res.LastModified, res.ContentType); err == nil {
return nil, resp
}
}
return req, nil
})
proxy.OnRequest(goproxy.ReqHostMatches(regexp.MustCompile("^.*:80$"))).
HijackConnect(func(req *http.Request, client net.Conn, ctx *goproxy.ProxyCtx) {
defer func() {
Expand All @@ -61,8 +112,6 @@ func listenAndServe(opts ServerOptions) (*http.Server, error) {
}
client.Close()
}()
url := req.URL.String()
ctx.Logf("URL: %s", url)
clientBuf := bufio.NewReadWriter(bufio.NewReader(client), bufio.NewWriter(client))
remote, err := net.Dial("tcp", req.URL.Host)
if err != nil {
Expand Down

0 comments on commit 2ec809f

Please sign in to comment.