示例#1
0
文件: config.go 项目: jmcarbo/srvdir
func defaultPath() string {
	user, err := user.Current()

	// user.Current() does not work on linux when cross compilling because
	// it requires CGO; use os.Getenv("HOME") hack until we compile natively
	homeDir := os.Getenv("HOME")
	if err != nil {
		log.Warn("Failed to get user's home directory: %s. Using $HOME: %s", err.Error(), homeDir)
	} else {
		homeDir = user.HomeDir
	}

	return filepath.Join(homeDir, ".srvdir")
}
示例#2
0
文件: main.go 项目: jmcarbo/srvdir
func Main() {
	// parse command line opts
	opts, err := parseArgs()
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	// set up logging
	if opts.logto != "" {
		log.LogTo(opts.logto)
	}

	// set up tls configuration
	tlsName, _, err := net.SplitHostPort(opts.serverAddr)
	if err != nil {
		fmt.Printf("Failed to parse server address: %v: %v\n", opts.serverAddr, err)
		os.Exit(1)
	}

	tlsCfg, err := tls.ClientTrusted(tlsName)
	if err != nil {
		fmt.Printf("Failed to create TLS configuration for name %v: %v\n", tlsName, err)
		os.Exit(1)
	}

	// load configuration file
	config, err := LoadConfiguration(opts.configPath)
	if err != nil {
		fmt.Printf("Error loading confuration: %v", err)
		os.Exit(1)
	}

	// override authtoken with command line option
	if opts.authtoken != "" {
		config.AuthToken = opts.authtoken
	}

	// connect to srvdir service
	authExtra := srvdir_proto.NewAuthExtra(config.AuthToken, version)
	c, err := tunnel.DialTLSReconnecting("tcp", opts.serverAddr, tlsCfg, authExtra)
	if err != nil {
		fmt.Printf("Failed to setup tunnel connection to %v: %v\n", opts.serverAddr, err)
		os.Exit(1)
	}

	if err := SaveAuthToken(opts.configPath, config.AuthToken); err != nil {
		log.Warn("Failed to save authtoken to config file: %v", err)
	}

	routes := make([]Route, len(opts.dirs))

	for i, d := range opts.dirs {
		httpOpts := &proto.HTTPOptions{Subdomain: d.subdomain, Auth: opts.auth}
		tun, err := c.ListenHTTPS(httpOpts, nil)
		if err != nil {
			fmt.Printf("Failed to listen on subdomain '%v': %v\n", d.subdomain, err)
			os.Exit(1)
		}

		path := d.path
		go func() {
			log.Info("Serving %s on %s", path, tun.Addr().String())
			fs := FileServer(Dir(path), opts.index, opts.readOnly, opts.tmpl)
			if err := http.Serve(tun, fs); err != nil {
				fmt.Printf("Failed to start static http server for directory %v: %v\n", path, err)
				os.Exit(1)
			}
		}()

		routes[i] = Route{
			addr: tun.Addr().String(),
			path: path,
		}
	}

	// run the console UI
	if opts.logto != "stdout" {
		ui(routes)
	} else {
		select {}
	}
}