Esempio n. 1
0
func ticketCommand(args []string) {
	cl := NewClient(serverAddress(), moleIni.Get("server", "fingerprint"))
	cl.Ticket = moleIni.Get("server", "ticket")
	tic, err := cl.ParseTicket()
	fatalErr(err)

	infof(msgTicketExplanation, ansi.Cyan(tic.User), ansi.Cyan(time.Time(tic.Validity).String()))
	for _, ip := range tic.IPs {
		infoln("  * ", ansi.Cyan(ip))
	}
}
Esempio n. 2
0
func sendForwards(fwdChan chan<- conf.ForwardLine, cfg *conf.Config) {
	for _, fwd := range cfg.Forwards {
		infoln(ansi.Bold(ansi.Cyan(fwd.Name)))
		for _, cmt := range fwd.Comments {
			infoln(ansi.Cyan("  ; " + cmt))
		}
		for _, line := range fwd.Lines {
			infoln("  " + line.String())
			fwdChan <- line
		}
	}
}
Esempio n. 3
0
File: cmd-ls.go Progetto: calmh/mole
func tableFormatter(cell string, row, col, flags int) string {
	if row == 0 {
		return ansi.Underline(cell)
	} else if col == 0 {
		return ansi.Bold(ansi.Cyan(cell))
	} else if col == 1 && (strings.HasSuffix(cell, "U") || strings.HasSuffix(cell, "E")) {
		return ansi.Red(cell)
	} else if flags&table.Truncated != 0 {
		return cell[:len(cell)-1] + ansi.Red(">")
	}
	return cell
}
Esempio n. 4
0
File: usage.go Progetto: calmh/mole
func mainUsage(w io.Writer) {
	tw := tabwriter.NewWriter(w, 2, 4, 2, ' ', 0)

	fmt.Fprintln(w, ansi.Bold("Commands:"))
	var cmds []string
	for _, cmd := range commandList {
		cmds = append(cmds, cmd.name)
	}
	sort.Strings(cmds)
	for _, name := range cmds {
		cmd := commandMap[name]
		if sn := cmd.descr; sn != "" {
			alias := ""
			// Ignore undocumented commands
			if len(cmd.aliases) > 0 {
				alias = " (" + strings.Join(cmd.aliases, ", ") + ")"
			}
			tw.Write([]byte(fmt.Sprintf("  %s%s\t%s\n", ansi.Bold(ansi.Cyan(name)), ansi.Cyan(alias), sn)))
		}
	}
	tw.Flush()
	fmt.Fprintln(w, "\n  Commands can be abbreviated to their unique prefix.\n")

	fmt.Fprintln(w, ansi.Bold("Examples:"))
	examples := [][]string{
		{"  mole ls", "# show all available tunnels"},
		{"  mole l foo", "# show all available tunnels matching the regexp \"foo\""},
		{"  mole show foo", "# show the hosts and forwards in the tunnel \"foo\""},
		{"  sudo mole dig foo", "# dig the tunnel \"foo\""},
		{"  sudo mole -d d foo", "# dig the tunnel \"foo\", while showing debug output"},
		{"  mole push foo.ini", "# create or update the \"foo\" tunnel from a local file"},
		{"  mole install", "# list packages available for installation"},
		{"  mole ins vpnc", "# install a package named vpnc"},
		{"  mole up -force", "# perform a forced up/downgrade to the server version"},
	}
	optionTable(w, examples)
	w.Write([]byte("\n"))
}
Esempio n. 5
0
func commandDig(args []string) {
	fs := flag.NewFlagSet("dig", flag.ExitOnError)
	local := fs.Bool("l", false, "Local file, not remote tunnel definition")
	qualify := fs.Bool("q", false, "Use <host>.<tunnel> for host aliases instead of just <host>")
	noVerify := fs.Bool("n", false, "Don't verify connectivity")
	direct := fs.Bool("d", false, "Use direct connectivity, bypassing VPN/SSH")
	fs.DurationVar(&keepaliveInterval, "keepalive", keepaliveInterval, "SSH server alive timeout")
	fs.Usage = usageFor(fs, msgDigUsage)
	fs.Parse(args)
	args = fs.Args()

	if l := len(args); l < 1 || l > 2 {
		fs.Usage()
		exit(3)
	}

	// Fail early in case we don't have root since it's always required on
	// platforms where it matters
	requireRoot("dig")

	cfg := loadTunnel(args[0], *local)

	for _, cmt := range cfg.Comments {
		infoln(ansi.Cyan("; " + cmt))
	}
	for _, cmt := range cfg.General.Comments {
		infoln(ansi.Cyan("; " + cmt))
	}

	var addrs []string
	if remapIntfs {
		cfg.Remap()
	} else {
		addrs = missingAddresses(cfg)
		if len(addrs) > 0 {
			addAddresses(addrs)
		}
		atExit(func() {
			addrs := extraneousAddresses(cfg)
			if len(addrs) > 0 {
				removeAddresses(addrs)
			}
		})
	}

	if mh := fs.Arg(1); mh != "" {
		_, ok := cfg.HostsMap[mh]
		if !ok {
			fatalf(msgDigNoHost, mh)
		}
		cfg.General.Main = mh
		warnln(msgDigWarnMainHost)
		*noVerify = true
	}
	infoln(sshPathStr(cfg.General.Main, cfg), "...")

	var vpn VPN
	var err error

	if !*direct {
		if cfg.Vpnc != nil {
			vpn, err = startVpn("vpnc", cfg)
			fatalErr(err)
		} else if cfg.OpenConnect != nil {
			vpn, err = startVpn("openconnect", cfg)
			fatalErr(err)
		}
		atExit(func() {
			if vpn != nil {
				vpn.Stop()
			}
		})
	}

	var dialer Dialer = proxy.Direct
	if !*direct {
		if mh := cfg.General.Main; mh != "" {
			sshConn, err := sshHost(mh, cfg)
			fatalErr(err)
			dialer = sshConn

			startKeepalive(dialer)
		}
	}

	fwdChan := startForwarder(dialer)
	sendForwards(fwdChan, cfg)

	domain := args[0]
	if *local {
		domain = strings.TrimSuffix(domain, ".ini")
	}

	setupHostsFile(domain, cfg, *qualify)
	atExit(func() {
		restoreHostsFile(domain, *qualify)
	})

	if !*noVerify {
		go verify(dialer, cfg)
	}

	go autoUpgrade()

	shell(fwdChan, cfg, dialer)

	okln("Done")
	printTotalStats()
}
Esempio n. 6
0
File: shell.go Progetto: calmh/mole
func shell(fwdChan chan<- conf.ForwardLine, cfg *conf.Config, dialer Dialer) {
	help := func() {
		infoln("Available commands:")
		infoln("  help, ?                          - show help")
		infoln("  quit, ^D                         - stop forwarding and exit")
		infoln("  test                             - test each forward for connection")
		infoln("  stat                             - show forwarding statistics")
		infoln("  debug                            - enable debugging")
		infoln("  fwd srcip:srcport dstip:dstport  - add forward")
	}

	term := liner.NewLiner()
	atExit(func() {
		term.Close()
	})

	// Receive commands

	commands := make(chan string)
	next := make(chan bool)
	go func() {
		for {
			prompt := "mole> "
			if debugEnabled {
				prompt = "(debug) mole> "
			}
			cmd, err := term.Prompt(prompt)
			if err == io.EOF {
				fmt.Println("quit")
				commands <- "quit"
				return
			}

			if cmd != "" {
				commands <- cmd
				term.AppendHistory(cmd)
				_, ok := <-next
				if !ok {
					return
				}
			}
		}
	}()

	// Catch ^C and treat as "quit" command

	sigchan := make(chan os.Signal, 1)
	signal.Notify(sigchan, os.Interrupt)
	go func() {
		<-sigchan
		fmt.Println("quit")
		commands <- "quit"
	}()

	// Handle commands

	for {
		cmd := <-commands

		parts := strings.SplitN(cmd, " ", -1)

		switch parts[0] {
		case "quit":
			close(next)
			return
		case "help", "?":
			help()
		case "stat":
			printStats()
		case "test":
			results := testForwards(dialer, cfg)
			for res := range results {
				infof(ansi.Bold(ansi.Cyan(res.name)))
				for _, line := range res.results {
					if line.err == nil {
						infof("%22s %s in %.02f ms", line.dst, ansi.Bold(ansi.Green("-ok-")), line.ms)
					} else {
						infof("%22s %s in %.02f ms (%s)", line.dst, ansi.Bold(ansi.Red("fail")), line.ms, line.err)
					}
				}
			}
		case "debug":
			infoln(msgDebugEnabled)
			debugEnabled = true
		case "fwd":
			if len(parts) != 3 {
				warnf(msgErrIncorrectFwd, cmd)
				break
			}

			src := strings.SplitN(parts[1], ":", 2)
			if len(src) != 2 {
				warnf(msgErrIncorrectFwdSrc, parts[1])
				break
			}

			var ipExists bool
			for _, ip := range currentAddresses() {
				if ip == src[0] {
					ipExists = true
					break
				}
			}
			if !ipExists {
				warnf(msgErrIncorrectFwdIP, src[0])
				break
			}

			dst := strings.SplitN(parts[2], ":", 2)
			if len(dst) != 2 {
				warnf(msgErrIncorrectFwdDst, parts[2])
				break
			}

			srcp, err := strconv.Atoi(src[1])
			if err != nil {
				warnln(err)
				break
			}
			if srcp < 1024 {
				warnf(msgErrIncorrectFwdPriv, srcp)
				break
			}

			dstp, err := strconv.Atoi(dst[1])
			if err != nil {
				warnln(err)
				break
			}
			fwd := conf.ForwardLine{
				SrcIP:   src[0],
				SrcPort: srcp,
				DstIP:   dst[0],
				DstPort: dstp,
			}
			okln("add", fwd)
			fwdChan <- fwd
		default:
			warnf(msgErrNoSuchCommand, parts[0])
		}

		next <- true
	}
}