-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapt_proxy_controller.go
78 lines (70 loc) · 1.66 KB
/
apt_proxy_controller.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package proxy
import (
"fmt"
"github.com/gin-gonic/gin"
"io"
"log"
"net/http"
"os"
"path"
"path/filepath"
"proxynd/configs"
"proxynd/helpers"
)
func AptProxy(c *gin.Context) {
log.Printf("Access proxy apt\n")
pathOs := c.Param("osType")
requestPath := c.Param("requestPath")
// fixme di
storageDir := helpers.GetStorageDir()
globalConfig := configs.GlobalConfig{}
globalConfig.ReadConfig()
config := configs.AptProxyConfig{}
config.ReadConfig()
//basedir := os.Getenv("STORAGE_DIR")
//if basedir == "" {
// var err error
// basedir, err = homedir.Dir()
// if err != nil {
// log.Fatal(err)
// }
//}
// Create the file
filefullpath := path.Join(storageDir, config.Path, requestPath)
filename := filepath.Base(filefullpath)
if _, err := os.Stat(filefullpath); os.IsNotExist(err) {
dirpath := filepath.Dir(filefullpath)
os.MkdirAll(dirpath, os.ModePerm)
out, err := os.Create(filefullpath)
if err != nil {
panic(err)
return
}
defer out.Close()
// Get the data
proxy := config.Proxies[pathOs]
for s, server := range proxy {
fmt.Printf("for moon %d\n", s)
resp, err := http.Get(helpers.JoinURL(server.URL, requestPath))
if err != nil {
log.Fatal(err)
return
}
//fmt.Println(resp.Header)
fmt.Println(resp.StatusCode)
// Writer the body to file
_, err = io.Copy(out, resp.Body)
if err != nil {
log.Fatal(err)
return
}
resp.Body.Close()
break
}
}
c.Header("Content-Description", "File Transfer")
c.Header("Content-Transfer-Encoding", "binary")
c.Header("Content-Disposition", "attachment; filename="+filename)
c.Header("Content-Type", "application/octet-stream")
c.File(filefullpath)
}