forked from lox/apt-proxy
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
118 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,10 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
|
||
"github.com/soulteary/apt-proxy/cli" | ||
"github.com/soulteary/apt-proxy/linux" | ||
) | ||
|
||
const ( | ||
DEFAULT_HOST = "0.0.0.0" | ||
DEFAULT_PORT = "3142" | ||
DEFAULT_CACHE_DIR = "./.aptcache" | ||
DEFAULT_MIRROR = "" // "https://mirrors.tuna.tsinghua.edu.cn/ubuntu/" | ||
DEFAULT_TYPE = linux.LINUX_DISTROS_UBUNTU | ||
DEFAULT_DEBUG = false | ||
) | ||
|
||
var version string | ||
|
||
func parseFlags() (appFlags cli.AppFlags) { | ||
var ( | ||
host string | ||
port string | ||
types string | ||
) | ||
flag.StringVar(&host, "host", DEFAULT_HOST, "the host to bind to") | ||
flag.StringVar(&port, "port", DEFAULT_PORT, "the port to bind to") | ||
flag.StringVar(&types, "type", DEFAULT_TYPE, "select the type of system to cache: ubuntu/debian") | ||
flag.BoolVar(&appFlags.Debug, "debug", DEFAULT_DEBUG, "whether to output debugging logging") | ||
flag.StringVar(&appFlags.CacheDir, "cachedir", DEFAULT_CACHE_DIR, "the dir to store cache data in") | ||
flag.StringVar(&appFlags.Mirror, "mirror", DEFAULT_MIRROR, "the mirror for fetching packages") | ||
flag.Parse() | ||
|
||
if types != linux.LINUX_DISTROS_UBUNTU && types != linux.LINUX_DISTROS_DEBIAN { | ||
types = linux.LINUX_DISTROS_UBUNTU | ||
} | ||
|
||
appFlags.Types = types | ||
appFlags.Listen = host + ":" + port | ||
appFlags.Version = version | ||
|
||
return appFlags | ||
} | ||
|
||
func main() { | ||
cli.Parse(parseFlags()) | ||
flags := cli.ParseFlags() | ||
cli.Daemon(&flags) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,43 @@ | ||
package cli | ||
|
||
import ( | ||
"log" | ||
"net/http" | ||
"flag" | ||
|
||
"github.com/soulteary/apt-proxy/pkgs/httpcache" | ||
"github.com/soulteary/apt-proxy/pkgs/httplog" | ||
"github.com/soulteary/apt-proxy/proxy" | ||
"github.com/soulteary/apt-proxy/linux" | ||
) | ||
|
||
type AppFlags struct { | ||
Debug bool | ||
Version string | ||
CacheDir string | ||
Mirror string | ||
Types string | ||
Listen string | ||
} | ||
|
||
func Parse(appFlags AppFlags) { | ||
log.Printf("running apt-proxy %s", appFlags.Version) | ||
|
||
if appFlags.Debug { | ||
log.Printf("enable debug: true") | ||
httpcache.DebugLogging = true | ||
} | ||
const ( | ||
DEFAULT_HOST = "0.0.0.0" | ||
DEFAULT_PORT = "3142" | ||
DEFAULT_CACHE_DIR = "./.aptcache" | ||
DEFAULT_MIRROR = "" // "https://mirrors.tuna.tsinghua.edu.cn/ubuntu/" | ||
DEFAULT_TYPE = linux.LINUX_DISTROS_UBUNTU | ||
DEFAULT_DEBUG = false | ||
) | ||
|
||
cache, err := httpcache.NewDiskCache(appFlags.CacheDir) | ||
if err != nil { | ||
log.Fatal(err) | ||
var version string | ||
|
||
func ParseFlags() (appFlags AppFlags) { | ||
var ( | ||
host string | ||
port string | ||
types string | ||
) | ||
flag.StringVar(&host, "host", DEFAULT_HOST, "the host to bind to") | ||
flag.StringVar(&port, "port", DEFAULT_PORT, "the port to bind to") | ||
flag.StringVar(&types, "type", DEFAULT_TYPE, "select the type of system to cache: ubuntu/debian") | ||
flag.BoolVar(&appFlags.Debug, "debug", DEFAULT_DEBUG, "whether to output debugging logging") | ||
flag.StringVar(&appFlags.CacheDir, "cachedir", DEFAULT_CACHE_DIR, "the dir to store cache data in") | ||
flag.StringVar(&appFlags.Mirror, "mirror", DEFAULT_MIRROR, "the mirror for fetching packages") | ||
flag.Parse() | ||
|
||
if types != linux.LINUX_DISTROS_UBUNTU && types != linux.LINUX_DISTROS_DEBIAN { | ||
types = linux.LINUX_DISTROS_UBUNTU | ||
} | ||
|
||
ap := proxy.NewAptProxyFromDefaults(appFlags.Mirror, appFlags.Types) | ||
ap.Handler = httpcache.NewHandler(cache, ap.Handler) | ||
|
||
logger := httplog.NewResponseLogger(ap.Handler) | ||
logger.DumpRequests = appFlags.Debug | ||
logger.DumpResponses = appFlags.Debug | ||
logger.DumpErrors = appFlags.Debug | ||
ap.Handler = logger | ||
appFlags.Types = types | ||
appFlags.Listen = host + ":" + port | ||
appFlags.Version = version | ||
|
||
log.Printf("proxy listening on %s", appFlags.Listen) | ||
log.Fatal(http.ListenAndServe(appFlags.Listen, ap)) | ||
return appFlags | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package cli | ||
|
||
import ( | ||
"log" | ||
"net/http" | ||
|
||
"github.com/soulteary/apt-proxy/pkgs/httpcache" | ||
"github.com/soulteary/apt-proxy/pkgs/httplog" | ||
"github.com/soulteary/apt-proxy/proxy" | ||
) | ||
|
||
type AppFlags struct { | ||
Debug bool | ||
Version string | ||
CacheDir string | ||
Mirror string | ||
Types string | ||
Listen string | ||
} | ||
|
||
func initStore(appFlags AppFlags) (cache httpcache.Cache, err error) { | ||
cache, err = httpcache.NewDiskCache(appFlags.CacheDir) | ||
if err != nil { | ||
return cache, err | ||
} | ||
return cache, nil | ||
} | ||
|
||
func initProxy(appFlags AppFlags, cache httpcache.Cache) (ap *proxy.AptProxy) { | ||
ap = proxy.NewAptProxyFromDefaults(appFlags.Mirror, appFlags.Types) | ||
ap.Handler = httpcache.NewHandler(cache, ap.Handler) | ||
return ap | ||
} | ||
|
||
func initLogger(appFlags AppFlags, ap *proxy.AptProxy) { | ||
if appFlags.Debug { | ||
log.Printf("enable debug: true") | ||
httpcache.DebugLogging = true | ||
} | ||
logger := httplog.NewResponseLogger(ap.Handler) | ||
logger.DumpRequests = appFlags.Debug | ||
logger.DumpResponses = appFlags.Debug | ||
logger.DumpErrors = appFlags.Debug | ||
ap.Handler = logger | ||
} | ||
|
||
func StartServer(appFlags *AppFlags, ap *proxy.AptProxy) { | ||
log.Printf("proxy listening on %s", appFlags.Listen) | ||
log.Fatal(http.ListenAndServe(appFlags.Listen, ap)) | ||
} | ||
|
||
func Daemon(appFlags *AppFlags) { | ||
log.Printf("running apt-proxy %s", appFlags.Version) | ||
|
||
cache, err := initStore(*appFlags) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
ap := initProxy(*appFlags, cache) | ||
|
||
initLogger(*appFlags, ap) | ||
|
||
StartServer(&*appFlags, ap) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package cli | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestInit(t *testing.T) { | ||
flags := ParseFlags() | ||
|
||
cache, err := initStore(flags) | ||
if err != nil { | ||
t.Fatal("Init Store Failed") | ||
} | ||
|
||
ap := initProxy(flags, cache) | ||
initLogger(flags, ap) | ||
} |