示例#1
1
func childMain() error {
	var err error

	pipe := os.NewFile(uintptr(3), "pipe")
	if pipe != nil {
		defer pipe.Close()
		if err == nil {
			pipe.Write([]byte{DaemonSuccess})
		} else {
			pipe.Write([]byte{DaemonFailure})
		}
	}

	signal.Ignore(syscall.SIGCHLD)

	syscall.Close(0)
	syscall.Close(1)
	syscall.Close(2)

	syscall.Setsid()
	syscall.Umask(022)

	// syscall.Chdir("/")

	return nil
}
示例#2
0
func OnInterrupt(fn func(), onExitFunc func()) {
	// deal with control+c,etc
	signalChan := make(chan os.Signal, 1)
	// controlling terminal close, daemon not exit
	signal.Ignore(syscall.SIGHUP)
	signal.Notify(signalChan,
		os.Interrupt,
		os.Kill,
		syscall.SIGALRM,
		// syscall.SIGHUP,
		// syscall.SIGINFO, this causes windows to fail
		syscall.SIGINT,
		syscall.SIGTERM,
		// syscall.SIGQUIT, // Quit from keyboard, "kill -3"
	)
	go func() {
		for _ = range signalChan {
			fn()
			if onExitFunc != nil {
				onExitFunc()
			}
			os.Exit(0)
		}
	}()
}
示例#3
0
文件: app.go 项目: hironobu-s/swiftfs
func afterDaemonize(err error) {
	// Ignore SIGCHLD signal
	signal.Ignore(syscall.SIGCHLD)

	// Close STDOUT, STDIN, STDERR
	syscall.Close(0)
	syscall.Close(1)
	syscall.Close(2)

	// Become the process group leader
	syscall.Setsid()

	// // Clear umask
	syscall.Umask(022)

	// // chdir for root directory
	syscall.Chdir("/")

	// Notify that the child process started successfuly
	pipe := os.NewFile(uintptr(3), "pipe")
	if pipe != nil {
		defer pipe.Close()
		if err == nil {
			pipe.Write([]byte{DAEMONIZE_SUCCESS})
		} else {
			pipe.Write([]byte{DAEMONIZE_FAIL})
		}
	}
}
示例#4
0
func main() {
	flag.Usage = usage
	flag.Parse()
	if flag.NArg() < 1 {
		usage()
	}

	if isatty(1) {
		dofile()
	}

	if isatty(2) {
		os.Stderr = os.Stdout
	}

	signal.Ignore(syscall.SIGHUP)

	args := flag.Args()
	cmd := exec.Command(args[0], args[1:]...)
	cmd.Stdin = os.Stdin
	cmd.Stderr = os.Stderr
	cmd.Stdout = os.Stdout

	err := cmd.Run()
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		if _, ok := err.(*exec.ExitError); ok {
			os.Exit(126)
		}
		os.Exit(127)
	}
}
示例#5
0
文件: notify.go 项目: jdef/gotrig
func Notify(path, s string) (int, error) {
	notifyLock.Lock()
	signal.Ignore(syscall.SIGPIPE)
	defer func() {
		signal.Reset(syscall.SIGPIPE)
		notifyLock.Unlock()
	}()
	return notifyNosig(path, s)
}
示例#6
0
func main() {
	flag.Usage = usage
	flag.Parse()

	if *sigint {
		signal.Ignore(os.Interrupt)
	}

	mode := os.O_CREATE | os.O_WRONLY
	if *append_ {
		mode |= os.O_APPEND
	} else {
		mode |= os.O_TRUNC
	}

	var files []*os.File
	var writers []*bufio.Writer
	for _, name := range flag.Args() {
		f, err := os.OpenFile(name, mode, 0666)
		if ck(err) {
			continue
		}
		files = append(files, f)
		writers = append(writers, bufio.NewWriter(f))
	}
	writers = append(writers, bufio.NewWriter(os.Stdout))

	for {
		var buf [8192]byte

		n, err := os.Stdin.Read(buf[:])
		if n > 0 {
			for _, w := range writers {
				w.Write(buf[:n])
			}
		}

		if err == io.EOF {
			break
		}

		ck(err)
	}

	for _, w := range writers {
		err := w.Flush()
		ck(err)
	}

	for _, f := range files {
		err := f.Close()
		ck(err)
	}

	os.Exit(status)
}
示例#7
0
文件: os_unix.go 项目: klaidliadon/oh
func InitSignalHandling() {
	signal.Ignore(syscall.SIGTTOU, syscall.SIGTTIN)

	signals := []os.Signal{syscall.SIGINT, syscall.SIGTSTP}
	incoming = make(chan os.Signal, len(signals))

	signal.Notify(incoming, signals...)

	go broker()
}
示例#8
0
func initSignalHandling(cli ui) {
	signal.Ignore(unix.SIGTTOU, unix.SIGTTIN)

	signals := []os.Signal{unix.SIGINT, unix.SIGTSTP}
	incoming = make(chan os.Signal, len(signals))

	signal.Notify(incoming, signals...)

	go broker(cli)
}
示例#9
0
func TestForeground(t *testing.T) {
	signal.Ignore(syscall.SIGTTIN, syscall.SIGTTOU)

	tty, err := os.OpenFile("/dev/tty", os.O_RDWR, 0)
	if err != nil {
		t.Skipf("Can't test Foreground. Couldn't open /dev/tty: %s", err)
	}

	fpgrp := 0

	errno := syscall.Ioctl(tty.Fd(), syscall.TIOCGPGRP, uintptr(unsafe.Pointer(&fpgrp)))
	if errno != 0 {
		t.Fatalf("TIOCGPGRP failed with error code: %s", errno)
	}

	if fpgrp == 0 {
		t.Fatalf("Foreground process group is zero")
	}

	ppid, ppgrp := parent()

	cmd := create(t)

	cmd.proc.SysProcAttr = &syscall.SysProcAttr{
		Ctty:       int(tty.Fd()),
		Foreground: true,
	}
	cmd.Start()

	cpid, cpgrp := cmd.Info()

	if cpid == ppid {
		t.Fatalf("Parent and child have the same process ID")
	}

	if cpgrp == ppgrp {
		t.Fatalf("Parent and child are in the same process group")
	}

	if cpid != cpgrp {
		t.Fatalf("Child's process group is not the child's process ID")
	}

	cmd.Stop()

	errno = syscall.Ioctl(tty.Fd(), syscall.TIOCSPGRP, uintptr(unsafe.Pointer(&fpgrp)))
	if errno != 0 {
		t.Fatalf("TIOCSPGRP failed with error code: %s", errno)
	}

	signal.Reset()
}
示例#10
0
文件: tee.go 项目: u-root/u-root
//Parses all the flags and sets variables accordingly
func handleflags() int {

	flag.Parse()

	oflags := os.O_WRONLY | os.O_CREATE

	if *cat {
		oflags |= os.O_APPEND
	}

	if *ignore {
		signal.Ignore(os.Interrupt)
	}

	return oflags
}
示例#11
0
func main() {
	// store information about this fake process into JSON
	signal.Ignore()

	var data outputData
	data.PID = os.Getpid()
	data.Args = os.Args[1:]

	// validate command line arguments
	// expect them to look like
	//   fake-thing agent -config-dir=/some/path/to/some/dir
	if len(data.Args) == 0 {
		log.Fatal("expecting command as first argment")
	}

	var configDir string
	var recursors stringSlice
	flagSet := flag.NewFlagSet("", flag.ExitOnError)
	flagSet.StringVar(&configDir, "config-dir", "", "config directory")
	flagSet.Var(&recursors, "recursor", "recursor")
	flagSet.Parse(data.Args[1:])
	if configDir == "" {
		log.Fatal("missing required config-dir flag")
	}

	writeOutput(configDir, data)

	// read input options provided to us by the test
	var inputOptions struct {
		WaitForHUP bool
	}

	if optionsBytes, err := ioutil.ReadFile(filepath.Join(configDir, "options.json")); err == nil {
		json.Unmarshal(optionsBytes, &inputOptions)
	}

	fmt.Fprintf(os.Stdout, "some standard out")
	fmt.Fprintf(os.Stderr, "some standard error")

	if inputOptions.WaitForHUP {
		for {
			time.Sleep(time.Second)
		}
	}
}
示例#12
0
文件: tee.go 项目: sternix/commands
func main() {
	var (
		flags   int = (os.O_WRONLY | os.O_CREATE)
		exitval int
		files   []io.Writer = []io.Writer{os.Stdout}
	)

	appendFlag := flag.Bool("a", false, "Append the output to the files rather than overwriting them.")
	interruptFlag := flag.Bool("i", false, "Ignore the SIGINT signal.")
	flag.Usage = usage

	flag.Parse()

	if *interruptFlag {
		signal.Ignore(syscall.SIGINT)
	}

	if *appendFlag {
		flags |= os.O_APPEND
	} else {
		flags |= os.O_TRUNC
	}

	for _, arg := range flag.Args() {
		if arg == "-" {
			continue
		}

		if f, err := os.OpenFile(arg, flags, os.ModePerm); err != nil {
			log.Printf("%s - %v", arg, err)
			exitval = 1
		} else {
			defer f.Close()
			files = append(files, f)
		}
	}

	if _, err := io.Copy(io.MultiWriter(files...), os.Stdin); err != nil {
		log.Printf("%v", err)
		exitval = 1
	}

	os.Exit(exitval)
}
示例#13
0
func OnInterrupt(fn func()) {
	// deal with control+c,etc
	signalChan := make(chan os.Signal, 1)
	// controlling terminal close, daemon not exit
	signal.Ignore(syscall.SIGHUP)
	signal.Notify(signalChan,
		os.Interrupt,
		os.Kill,
		syscall.SIGALRM,
		// syscall.SIGHUP,
		syscall.SIGINT,
		syscall.SIGTERM,
		syscall.SIGQUIT)
	go func() {
		for _ = range signalChan {
			fn()
			os.Exit(0)
		}
	}()
}
示例#14
0
文件: daemon.go 项目: wxjs33/kfkcp
func Daemon(nochdir int) error {
	signal.Ignore(syscall.SIGINT, syscall.SIGHUP, syscall.SIGPIPE)

	if syscall.Getppid() == 1 {
		return nil
	}

	ret1, ret2, eno := syscall.RawSyscall(syscall.SYS_FORK, 0, 0, 0)
	if eno != 0 || ret2 < 0 {
		return errors.New("Fork fail")
	}

	/* exit parent */
	if ret1 > 0 {
		os.Exit(0)
	}

	_ = syscall.Umask(0)

	ret, err := syscall.Setsid()
	if ret < 0 || err != nil {
		return errors.New("Set sid failed")
	}

	if nochdir == 0 {
		err = os.Chdir("/")
		if err != nil {
			return errors.New("Chdir failed")
		}
	}

	file, err := os.OpenFile("/dev/null", os.O_RDWR, 0)
	if err == nil {
		fd := file.Fd()
		syscall.Dup2(int(fd), int(os.Stdin.Fd()))
		syscall.Dup2(int(fd), int(os.Stdout.Fd()))
		//	syscall.Dup2(int(fd), int(os.Stderr.Fd()))
	}

	return nil
}
示例#15
0
文件: daemon.go 项目: b4b4r07/misc
func childMain() {
	// 初期化処理があればここに
	var err error

	// 子プロセスの起動状態を親プロセスに通知する
	pipe := os.NewFile(uintptr(3), "pipe")
	if pipe != nil {
		defer pipe.Close()
		if err == nil {
			pipe.Write([]byte{DAEMON_SUCCESS})
		} else {
			pipe.Write([]byte{DAEMON_FAIL})
		}
	}

	// SIGCHILDを無視する
	signal.Ignore(syscall.SIGCHLD)

	// STDOUT, STDIN, STDERRをクローズ
	syscall.Close(0)
	syscall.Close(1)
	syscall.Close(2)

	// プロセスグループリーダーになる
	syscall.Setsid()

	// Umaskをクリア
	syscall.Umask(022)

	// / にchdirする
	syscall.Chdir("/")

	// main loop
	for {
		time.Sleep(1000 * time.Millisecond)
	}
}
示例#16
0
文件: main.go 项目: zaksoup/yoloist
func main() {
	// store information about this fake process into JSON
	signal.Ignore()

	var data outputData
	data.Path = os.Args[0]
	data.Args = os.Args[1:]

	// validate command line arguments
	// expect them to look like
	//   fake-thing agent -config-dir=/some/path/to/some/dir
	if len(data.Args) == 0 {
		log.Fatal("Expected at least 1 argument")
	}

	// fmt.Fprintf(os.Stdout, "some standard out")
	// fmt.Fprintf(os.Stderr, "some standard error")
	outputBytes, err := json.Marshal(data)
	if err != nil {
		panic(err)
	}

	fmt.Fprintf(os.Stdout, string(outputBytes))
}
示例#17
0
func main() {
	debug := os.Getenv("VAGRANT_DEBUG_LAUNCHER") != ""

	// Get the path to the executable. This path doesn't resolve symlinks
	// so we have to do that afterwards to find the real binary.
	path, err := osext.Executable()
	if err != nil {
		fmt.Fprintf(os.Stderr, "Failed to load Vagrant: %s\n", err)
		os.Exit(1)
	}
	if debug {
		log.Printf("launcher: path = %s", path)
	}
	for {
		fi, err := os.Lstat(path)
		if err != nil {
			fmt.Fprintf(os.Stderr, "Failed to stat executable: %s\n", err)
			os.Exit(1)
		}
		if fi.Mode()&os.ModeSymlink == 0 {
			break
		}

		// The executable is a symlink, so resolve it
		path, err = os.Readlink(path)
		if err != nil {
			fmt.Fprintf(os.Stderr, "Failed to load Vagrant: %s\n", err)
			os.Exit(1)
		}
		if debug {
			log.Printf("launcher: resolved symlink = %s", path)
		}
	}

	// Determine some basic directories that we use throughout
	path = filepath.Dir(filepath.Clean(path))
	installerDir := filepath.Dir(path)
	embeddedDir := filepath.Join(installerDir, "embedded")
	if debug {
		log.Printf("launcher: installerDir = %s", installerDir)
		log.Printf("launcher: embeddedDir = %s", embeddedDir)
	}

	// Find the Vagrant gem
	gemPaths, err := filepath.Glob(
		filepath.Join(embeddedDir, "gems", "gems", "vagrant-*"))
	if err != nil {
		fmt.Fprintf(os.Stderr, "Failed to find Vagrant: %s\n", err)
		os.Exit(1)
	}
	if debug {
		log.Printf("launcher: gemPaths (initial) = %#v", gemPaths)
	}
	for i := 0; i < len(gemPaths); i++ {
		fullPath := filepath.Join(gemPaths[i], "lib", "vagrant", "pre-rubygems.rb")
		if _, err := os.Stat(fullPath); err != nil {
			if debug {
				log.Printf("launcher: bad gemPath += %s", fullPath)
			}

			gemPaths = append(gemPaths[:i], gemPaths[i+1:]...)
			i--
		}
	}
	if len(gemPaths) == 0 {
		fmt.Fprintf(os.Stderr, "Failed to find Vagrant!\n")
		os.Exit(1)
	}
	gemPath := gemPaths[len(gemPaths)-1]
	vagrantExecutable := filepath.Join(gemPath, "bin", "vagrant")
	if debug {
		log.Printf("launcher: gemPaths (final) = %#v", gemPaths)
		log.Printf("launcher: gemPath = %s", gemPath)
	}

	// Setup the CPP/LDFLAGS so that native extensions can be
	// properly compiled into the Vagrant environment.
	cppflags := "-I" + filepath.Join(embeddedDir, "include")
	ldflags := "-L" + filepath.Join(embeddedDir, "lib")
	if original := os.Getenv("CPPFLAGS"); original != "" {
		cppflags = original + " " + cppflags
	}
	if original := os.Getenv("LDFLAGS"); original != "" {
		ldflags = original + " " + ldflags
	}

	// Set the PATH to include the proper paths into our embedded dir
	path = os.Getenv("PATH")
	if runtime.GOOS == "windows" {
		path = fmt.Sprintf(
			"%s;%s;%s",
			filepath.Join(embeddedDir, "bin"),
			filepath.Join(embeddedDir, "gnuwin32", "bin"),
			path)
	} else {
		path = fmt.Sprintf("%s:%s",
			filepath.Join(embeddedDir, "bin"), path)
	}

	// Allow users to specify a custom SSL cert
	sslCertFile := os.Getenv("SSL_CERT_FILE")
	if sslCertFile == "" {
		sslCertFile = filepath.Join(embeddedDir, "cacert.pem")
	}

	newEnv := map[string]string{
		// Setup the environment to prefer our embedded dir over
		// anything the user might have setup on his/her system.
		"CPPFLAGS":      cppflags,
		"GEM_HOME":      filepath.Join(embeddedDir, "gems"),
		"GEM_PATH":      filepath.Join(embeddedDir, "gems"),
		"GEMRC":         filepath.Join(embeddedDir, "etc", "gemrc"),
		"LDFLAGS":       ldflags,
		"PATH":          path,
		"SSL_CERT_FILE": sslCertFile,

		// Environmental variables used by Vagrant itself
		"VAGRANT_EXECUTABLE":             vagrantExecutable,
		"VAGRANT_INSTALLER_ENV":          "1",
		"VAGRANT_INSTALLER_EMBEDDED_DIR": embeddedDir,
		"VAGRANT_INSTALLER_VERSION":      "2",
	}

	// Unset any RUBYOPT, we don't want this bleeding into our runtime
	newEnv["RUBYOPT"] = ""
	// Unset any RUBYLIB, we don't want this bleeding into our runtime
	newEnv["RUBYLIB"] = ""

	// Store the "current" environment so Vagrant can restore it when shelling
	// out.
	for _, value := range os.Environ() {
		idx := strings.IndexRune(value, '=')
		key := fmt.Sprintf("%s_%s", envPrefix, value[:idx])
		newEnv[key] = value[idx+1:]
	}
	if debug {
		keys := make([]string, 0, len(newEnv))
		for k, _ := range newEnv {
			keys = append(keys, k)
		}
		sort.Strings(keys)

		for _, k := range keys {
			log.Printf("launcher: env %q = %q", k, newEnv[k])
		}
	}

	// Set all the environmental variables
	for k, v := range newEnv {
		if err := os.Setenv(k, v); err != nil {
			fmt.Fprintf(os.Stderr, "Error setting env var %s: %s\n", k, err)
			os.Exit(1)
		}
	}

	// Determine the path to Ruby and then start the Vagrant process
	rubyPath := filepath.Join(embeddedDir, "bin", "ruby")
	if runtime.GOOS == "windows" {
		rubyPath += ".exe"
	}

	// Prior to starting the command, we ignore interrupts. Vagrant itself
	// handles these, so the launcher should just wait until that exits.
	signal.Ignore(os.Interrupt)

	cmd := exec.Command(rubyPath)
	cmd.Args = make([]string, len(os.Args)+1)
	cmd.Args[0] = "ruby"
	cmd.Args[1] = filepath.Join(gemPath, "lib", "vagrant", "pre-rubygems.rb")
	copy(cmd.Args[2:], os.Args[1:])
	if debug {
		log.Printf("launcher: rubyPath = %s", rubyPath)
		log.Printf("launcher: args = %#v", cmd.Args)
	}

	cmd.Stdin = os.Stdin
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	if err := cmd.Start(); err != nil {
		fmt.Fprintf(os.Stderr, "Exec error: %s\n", err)
		os.Exit(1)
	}

	exitCode := 0
	if err := cmd.Wait(); err != nil {
		if exiterr, ok := err.(*exec.ExitError); ok {
			// The program has exited with an exit code != 0

			// This works on both Unix and Windows. Although package
			// syscall is generally platform dependent, WaitStatus is
			// defined for both Unix and Windows and in both cases has
			// an ExitStatus() method with the same signature.
			if status, ok := exiterr.Sys().(syscall.WaitStatus); ok {
				exitCode = status.ExitStatus()
			}
		}
	}

	os.Exit(exitCode)
}
示例#18
0
func main() {
	var fg, bgChild bool
	flag.BoolVar(&fg, "fg", false, "run in foreground")
	flag.BoolVar(&bgChild, "bg-child", false, "")

	var nfiles int = nfilesGet()
	var nfilesReq int
	flag.IntVar(&nfilesReq, "nfiles", nfiles, "maximum number of files")
	flag.IntVar(&nfilesReq, "n", nfiles, "maximum number of files")

	var port int = 20444
	flag.IntVar(&port, "port", port, "listening port")
	flag.IntVar(&port, "p", port, "listening port")

	var user string
	flag.StringVar(&user, "user", "", "run as user")
	flag.StringVar(&user, "u", "", "run as user")

	var watchDirectory string
	flag.StringVar(&watchDirectory, "watch-directory", "", "directory to watch for updates")
	flag.StringVar(&watchDirectory, "d", "", "directory to watch for updates")
	flag.StringVar(&watchDirectory, "update-directory", "", "")

	var pidFile string
	flag.StringVar(&pidFile, "pid-file", "", "file to write process PID")

	var logFile string
	flag.StringVar(&logFile, "log", "", "log file")
	flag.StringVar(&logFile, "log-file", "", "log file")

	flag.Parse()

	var logw io.Writer = ioutil.Discard
	if logFile != "" {
		logw, err := os.OpenFile(logFile, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0660)
		if err != nil {
			log.Fatal(err)
		}
		log.SetOutput(io.MultiWriter(logw, os.Stderr))
	}

	if nfiles != nfilesReq {
		nfiles = nfilesSet(nfilesReq)
		if nfiles < nfilesReq {
			log.Printf("limited to %d open files\n", nfiles)
		}
	}

	var listener net.Listener
	var err error
	if bgChild {
		listener, err = net.FileListener(os.NewFile(3, "listen-fd"))
	} else {
		listener, err = net.Listen("tcp", fmt.Sprintf(":%d", port))
	}
	if err != nil {
		log.Fatal(err)
	}

	if user != "" {
		userSet(user)
	}

	if watchDirectory != "" && directoryWatcher == nil {
		log.Fatalf("this platform does not support `--update-directory`")
	} else if watchDirectory != "" {
		go directoryWatcher(watchDirectory)
	}

	if pidFile != "" {
		err := ioutil.WriteFile(pidFile, []byte(fmt.Sprintf("%d\n", os.Getpid())), 0660)
		if err != nil {
			log.Fatal(err)
		}
	}

	if !fg && !bgChild {
		tcpListenFd, err := listener.(*net.TCPListener).File()
		if err != nil {
			log.Fatal(err)
		}
		args := []string{"--bg-child"}
		if logFile != "" {
			args = append(args, "--log-file", logFile)
		}
		if pidFile != "" {
			args = append(args, "--pid-file", pidFile)
		}
		if user != "" {
			args = append(args, "--user", user)
		}
		if watchDirectory != "" {
			args = append(args, "--watch-directory", watchDirectory)
		}
		cmd := exec.Command(os.Args[0], args...)
		cmd.ExtraFiles = []*os.File{tcpListenFd}
		if err = cmd.Start(); err != nil {
			log.Fatal(err)
		}
		os.Exit(0)
	}

	if bgChild {
		unix.Setpgid(0, 0)
		signal.Ignore(unix.SIGHUP)
		log.SetOutput(logw)
		os.Stdin.Close()
		os.Stdout.Close()
		os.Stderr.Close()
	}

	http.HandleFunc("/poll", PollRequest)
	http.HandleFunc("/update", UpdateRequest)
	log.Fatal(http.Serve(listener, nil))
}
示例#19
0
// StartWithCertificate runs a proxy on addr and configures a cert for MITM
func StartWithCertificate(proxyAddr string, cert string, key string) (*Martian, error) {
	flag.Set("logtostderr", "true")

	signal.Ignore(syscall.SIGPIPE)

	l, err := net.Listen("tcp", proxyAddr)
	if err != nil {
		return nil, err
	}

	mlog.Debugf("mobileproxy: started listener: %v", l.Addr())

	p := martian.NewProxy()

	mux := http.NewServeMux()
	p.SetMux(mux)

	if cert != "" && key != "" {
		tlsc, err := tls.X509KeyPair([]byte(cert), []byte(key))
		if err != nil {
			log.Fatal(err)
		}

		mlog.Debugf("mobileproxy: loaded cert and key")

		x509c, err := x509.ParseCertificate(tlsc.Certificate[0])
		if err != nil {
			log.Fatal(err)
		}

		mlog.Debugf("mobileproxy: parsed cert")

		mc, err := mitm.NewConfig(x509c, tlsc.PrivateKey)
		if err != nil {
			log.Fatal(err)
		}

		mc.SetValidity(12 * time.Hour)
		mc.SetOrganization("Martian Proxy")

		p.SetMITM(mc)

		mux.Handle("martian.proxy/authority.cer", martianhttp.NewAuthorityHandler(x509c))
		mlog.Debugf("mobileproxy: install cert from http://martian.proxy/authority.cer")
	}

	stack, fg := httpspec.NewStack("martian.mobileproxy")
	p.SetRequestModifier(stack)
	p.SetResponseModifier(stack)

	// add HAR logger
	hl := har.NewLogger()
	stack.AddRequestModifier(hl)
	stack.AddResponseModifier(hl)

	m := martianhttp.NewModifier()
	fg.AddRequestModifier(m)
	fg.AddResponseModifier(m)

	mlog.Debugf("mobileproxy: set martianhttp modifier")

	// Proxy specific handlers.
	// These handlers take precendence over proxy traffic and will not be intercepted.

	// Retrieve HAR logs
	mux.Handle("martian.proxy/logs", har.NewExportHandler(hl))
	mux.Handle("martian.proxy/logs/reset", har.NewResetHandler(hl))

	// Update modifiers.
	mux.Handle("martian.proxy/configure", m)
	mlog.Debugf("mobileproxy: configure with requests to http://martian.proxy/configure")

	// Verify assertions.
	vh := verify.NewHandler()
	vh.SetRequestVerifier(m)
	vh.SetResponseVerifier(m)
	mux.Handle("martian.proxy/verify", vh)
	mlog.Debugf("mobileproxy: check verifications with requests to http://martian.proxy/verify")

	// Reset verifications.
	rh := verify.NewResetHandler()
	rh.SetRequestVerifier(m)
	rh.SetResponseVerifier(m)
	mux.Handle("martian.proxy/verify/reset", rh)
	mlog.Debugf("mobileproxy: reset verifications with requests to http://martian.proxy/verify/reset")

	go p.Serve(l)
	mlog.Infof("mobileproxy: started proxy on listener")

	return &Martian{
		proxy:    p,
		listener: l,
		mux:      mux,
	}, nil
}
示例#20
0
func main() {
	// store information about this fake process into JSON
	signal.Ignore()

	var data outputData
	data.PID = os.Getpid()
	data.Args = os.Args[1:]

	// validate command line arguments
	// expect them to look like
	//   fake-thing agent -config-dir=/some/path/to/some/dir
	if len(data.Args) == 0 {
		log.Fatal("expecting command as first argment")
	}
	var configDir string
	flagSet := flag.NewFlagSet("", flag.ExitOnError)
	flagSet.StringVar(&configDir, "config-dir", "", "config directory")
	flagSet.Parse(data.Args[1:])
	if configDir == "" {
		log.Fatal("missing required config-dir flag")
	}

	writeOutput(configDir, data)
	defer writeOutput(configDir, data)

	// read input options provided to us by the test
	var inputOptions struct {
		Slow          bool
		WaitForHUP    bool
		StayAlive     bool
		RunClient     bool
		RunServer     bool
		Members       []string
		FailRPCServer bool
	}
	if optionsBytes, err := ioutil.ReadFile(filepath.Join(configDir, "options.json")); err == nil {
		json.Unmarshal(optionsBytes, &inputOptions)
	}

	fmt.Fprintf(os.Stdout, "some standard out")
	fmt.Fprintf(os.Stderr, "some standard error")

	if inputOptions.Slow {
		time.Sleep(10 * time.Second)
	}

	if inputOptions.WaitForHUP {
		for {
			time.Sleep(time.Second)
		}
	}

	if inputOptions.RunClient {
		fmt.Println("running client")
		ClientListener{
			Addr:    "127.0.0.1:8500",
			Members: inputOptions.Members,
		}.Serve()
	}

	tcpAddr := ""
	if !inputOptions.FailRPCServer {
		tcpAddr = "127.0.0.1:8400"
	}

	if inputOptions.RunServer {
		fmt.Println("running server")
		ServerListener{
			HTTPAddr:  "127.0.0.1:8500",
			TCPAddr:   tcpAddr,
			Members:   inputOptions.Members,
			StayAlive: inputOptions.StayAlive,
		}.Serve()
	}
}
示例#21
0
文件: safely.go 项目: ghedo/safely
func main() {
	log.SetFlags(0)

	usage := `safely [options]

Usage:
  safely [options] [--keys <keys>] --create
  safely [options] [--keys <keys>] --add <name>
  safely [options] --user <name>
  safely [options] --pass <name>
  safely [options] --2fa <name>
  safely [options] [--keys <keys>] --edit <name>
  safely [options] [--keys <keys>] --remove <name>
  safely [options] --search <query>
  safely [options] --check <pass>
  safely [options] --dump

Options:
  -c, --create                  Create a new password database.
  -a <name>, --add <name>       Add a new account to the database.
  -u <name>, --user <name>      Print the user name of the given account.
  -p <name>, --pass <name>      Print the password of the given account.
  -2 <name>, --2fa <name>       Print 2-factor auth token for the given account.
  -e <name>, --edit <name>      Modify the given account.
  -r <name>, --remove <name>    Remove the given account.
  -s <query>, --search <query>  Search the database for the given query.
  -C <pass>, --check <pass>     Estimate the given password's strength.
  -d, --dump                    Dump the database in JSON format.
  -D <file>, --db <file>        Use this database file [default: ~/.config/safely/passwords.db].
  -K <keys>, --keys <keys>      Use these space-separated GPG keys [default: ].
  -F, --fuzzy                   Enable non-exact (fuzzy) matches.
  -N, --print-newline           Force printing a trailing newline.
  -Q, --quiet                   Quiet output.
  -B, --no-backup               Do not create a backup of the database.
  -h, --help                    Show the program's help message and exit.`

	args, err := docopt.Parse(usage, nil, true, "", false)
	if err != nil {
		log.Fatalf("Invalid arguments: %s", err)
	}

	db_file, err := util.ExpandUser(args["--db"].(string))
	if err != nil {
		log.Fatalf("Error expanding home directory: %s", err)
	}

	keys_spec := args["--keys"].(string)

	fuzzy := args["--fuzzy"].(bool)
	quiet := args["--quiet"].(bool)
	no_backup := args["--no-backup"].(bool)

	signal.Ignore(os.Interrupt)

	gpg.Init(keys_spec)

	switch {
	case args["--create"].(bool) == true:
		mydb, err := db.Create(db_file)
		if err != nil {
			mydb.Remove()
			log.Fatalf("Error creating database: %s", err)
		}

		err = mydb.Sync(false)
		if err != nil {
			log.Fatalf("Error syncing DB: %s", err)
		}

		if quiet != true {
			log.Printf("Database '%s' created", db_file)
		}

	case args["--add"] != nil:
		account := args["--add"].(string)

		mydb, err := db.Open(db_file)
		if err != nil {
			log.Fatalf("Error opening database: %s", err)
		}

		if mydb.Search(account, false) != nil {
			log.Fatalf("Account '%s' already exists", account)
		}

		user, err := term.ReadLine(fmt.Sprintf(
			"Enter user name for '%s': ", account,
		))

		if err != nil {
			log.Fatalf("Error reading input: %s", err)
		}

		pass, err := term.ReadPass(fmt.Sprintf(
			"Enter password for '%s': ", account,
		))

		if err != nil {
			log.Fatalf("Error reading input: %s", err)
		}

		score := zxcvbn.PasswordStrength(pass, nil)

		log.Printf("It would take %v seconds (%s) to crack the password\n",
			score.CrackTime, score.CrackTimeDisplay)

		tfkey, err := term.ReadLine(fmt.Sprintf(
			"Enter 2-factor auth key for '%s': ", account,
		))

		if err != nil {
			log.Fatalf("Error reading input: %s", err)
		}

		mydb.Accounts[account] = &db.Account{
			user, pass, tfkey,
			time.Now().Format(time.RFC3339),
		}

		err = mydb.Sync(!no_backup)
		if err != nil {
			log.Fatalf("Error syncing DB: %s", err)
		}

		if quiet != true {
			log.Printf("Account '%s' added", account)
		}

	case args["--user"] != nil:
		query := args["--user"].(string)

		mydb, err := db.Open(db_file)
		if err != nil {
			log.Fatalf("Error opening database: %s", err)
		}

		account := mydb.Search(query, fuzzy)
		if account == nil {
			log.Fatalf("Account '%s' not found", query)
		}

		fmt.Print(account.User)

		if args["--print-newline"].(bool) ||
			terminal.IsTerminal(int(os.Stdout.Fd())) {
			fmt.Println("")
		}

	case args["--pass"] != nil:
		query := args["--pass"].(string)

		mydb, err := db.Open(db_file)
		if err != nil {
			log.Fatalf("Error opening database: %s", err)
		}

		account := mydb.Search(query, fuzzy)
		if account == nil {
			log.Fatalf("Account '%s' not found", query)
		}

		fmt.Print(account.Pass)

		if args["--print-newline"].(bool) ||
			terminal.IsTerminal(int(os.Stdout.Fd())) {
			fmt.Println("")
		}

	case args["--2fa"] != nil:
		query := args["--2fa"].(string)

		mydb, err := db.Open(db_file)
		if err != nil {
			log.Fatalf("Error opening database: %s", err)
		}

		account := mydb.Search(query, fuzzy)
		if account == nil {
			log.Fatalf("Account '%s' not found", query)
		}

		if account.TFKey == "" {
			log.Fatalf("No 2-factor auth key for '%s'", query)
		}

		secret, err := oath.Base32Secret(account.TFKey)

		totp := oath.TOTP(secret)

		fmt.Print(totp)

		if args["--print-newline"].(bool) ||
			terminal.IsTerminal(int(os.Stdout.Fd())) {
			fmt.Println("")
		}

	case args["--edit"] != nil:
		query := args["--edit"].(string)

		mydb, err := db.Open(db_file)
		if err != nil {
			log.Fatalf("Error opening database: %s", err)
		}

		account := mydb.Search(query, false)
		if account == nil {
			log.Fatalf("Account '%s' not found", query)
		}

		name, err := term.ReadLine(fmt.Sprintf(
			"Enter new account name [%s]: ",
			query,
		))

		if err != nil {
			log.Fatalf("Error reading input: %s", err)
		}

		if name == "" {
			name = query
		}

		user, err := term.ReadLine(fmt.Sprintf(
			"Enter new user name for '%s' [%s]: ",
			query, account.User,
		))

		if err != nil {
			log.Fatalf("Error reading input: %s", err)
		}

		if user == "" {
			user = account.User
		}

		pass, err := term.ReadPass(fmt.Sprintf(
			"Enter new password for '%s' [%s]: ",
			query, account.Pass,
		))

		if err != nil {
			log.Fatalf("Error reading input: %s", err)
		}

		if pass == "" {
			pass = account.Pass
		}

		tfkey, err := term.ReadLine(fmt.Sprintf(
			"Enter new 2-factor auth key for '%s' [%s]: ",
			query, account.TFKey,
		))

		if err != nil {
			log.Fatalf("Error reading input: %s", err)
		}

		if tfkey == "" {
			tfkey = account.TFKey
		}

		mydb.Accounts[name] = &db.Account{
			user, pass, tfkey,
			time.Now().Format(time.RFC3339),
		}

		if name != query {
			delete(mydb.Accounts, query)
		}

		err = mydb.Sync(!no_backup)
		if err != nil {
			log.Fatalf("Error syncing DB: %s", err)
		}

		if quiet != true {
			log.Printf("Account '%s' edited", query)
		}

	case args["--remove"] != nil:
		account := args["--remove"].(string)

		mydb, err := db.Open(db_file)
		if err != nil {
			log.Fatalf("Error opening database: %s", err)
		}

		if mydb.Search(account, false) == nil {
			log.Fatalf("Account '%s' not found", account)
		}

		delete(mydb.Accounts, account)

		err = mydb.Sync(!no_backup)
		if err != nil {
			log.Fatalf("Error syncing DB: %s", err)
		}

		if quiet != true {
			log.Printf("Account '%s' removed", account)
		}

	case args["--search"] != nil:
		query := args["--search"].(string)

		mydb, err := db.Open(db_file)
		if err != nil {
			log.Fatalf("Error opening database: %s", err)
		}

		for name, _ := range mydb.Accounts {
			if m, _ := regexp.MatchString(query, name); m {
				fmt.Println(name)
			}
		}

	case args["--check"] != nil:
		pass := args["--check"].(string)

		score := zxcvbn.PasswordStrength(pass, nil)

		log.Printf("It would take %v seconds (%s) to crack the password\n",
			score.CrackTime, score.CrackTimeDisplay)

	case args["--dump"].(bool) == true:
		plain, err := db.Dump(db_file)
		if err != nil {
			log.Fatalf("Error dumping database: %s", err)
		}

		fmt.Printf("%s\n", plain)

	default:
		log.Fatal("Invalid command")
	}
}
示例#22
0
func proxyGo(host *config.Host, dryRun bool) error {
	stats := ConnectionStats{
		CreatedAt: time.Now(),
	}
	connectHookArgs := ConnectHookArgs{
		Host:  host,
		Stats: &stats,
	}

	Logger.Debugf("Preparing host object")
	if err := hostPrepare(host); err != nil {
		return err
	}

	if dryRun {
		return fmt.Errorf("dry-run: Golang native TCP connection to '%s:%s'", host.HostName, host.Port)
	}

	// BeforeConnect hook
	Logger.Debugf("Calling BeforeConnect hooks")
	beforeConnectDrivers, err := host.Hooks.BeforeConnect.InvokeAll(connectHookArgs)
	if err != nil {
		Logger.Errorf("BeforeConnect hook failed: %v", err)
	}
	defer beforeConnectDrivers.Close()

	Logger.Debugf("Connecting to %s:%s", host.HostName, host.Port)
	conn, err := net.Dial("tcp", fmt.Sprintf("%s:%s", host.HostName, host.Port))
	if err != nil {
		// OnConnectError hook
		connectHookArgs.Error = err
		Logger.Debugf("Calling OnConnectError hooks")
		onConnectErrorDrivers, err := host.Hooks.OnConnectError.InvokeAll(connectHookArgs)
		if err != nil {
			Logger.Errorf("OnConnectError hook failed: %v", err)
		}
		defer onConnectErrorDrivers.Close()

		return err
	}
	Logger.Debugf("Connected to %s:%s", host.HostName, host.Port)
	stats.ConnectedAt = time.Now()

	// OnConnect hook
	Logger.Debugf("Calling OnConnect hooks")
	onConnectDrivers, err := host.Hooks.OnConnect.InvokeAll(connectHookArgs)
	if err != nil {
		Logger.Errorf("OnConnect hook failed: %v", err)
	}
	defer onConnectDrivers.Close()

	// Ignore SIGHUP
	signal.Ignore(syscall.SIGHUP)

	waitGroup := sync.WaitGroup{}
	result := exportReadWrite{}

	ctx, cancel := context.WithCancel(context.Background())
	ctx = context.WithValue(ctx, "sync", &waitGroup)

	waitGroup.Add(2)
	c1 := readAndWrite(ctx, conn, os.Stdout)
	c2 := readAndWrite(ctx, os.Stdin, conn)
	select {
	case result = <-c1:
		stats.WrittenBytes = result.written
	case result = <-c2:
	}
	if result.err != nil && result.err == io.EOF {
		result.err = nil
	}

	conn.Close()
	cancel()
	waitGroup.Wait()
	select {
	case res := <-c1:
		stats.WrittenBytes = res.written
	default:
	}

	stats.DisconnectedAt = time.Now()
	stats.ConnectionDuration = stats.DisconnectedAt.Sub(stats.ConnectedAt)
	averageSpeed := float64(stats.WrittenBytes) / stats.ConnectionDuration.Seconds()
	// round duraction
	stats.ConnectionDuration = ((stats.ConnectionDuration + time.Second/2) / time.Second) * time.Second
	stats.AverageSpeed = math.Ceil(averageSpeed*1000) / 1000
	// human
	stats.WrittenBytesHuman = humanize.Bytes(stats.WrittenBytes)
	connectionDurationHuman := humanize.RelTime(stats.DisconnectedAt, stats.ConnectedAt, "", "")
	stats.ConnectionDurationHuman = strings.Replace(connectionDurationHuman, "now", "0 sec", -1)
	stats.AverageSpeedHuman = humanize.Bytes(uint64(stats.AverageSpeed)) + "/s"

	// OnDisconnect hook
	Logger.Debugf("Calling OnDisconnect hooks")
	onDisconnectDrivers, err := host.Hooks.OnDisconnect.InvokeAll(connectHookArgs)
	if err != nil {
		Logger.Errorf("OnDisconnect hook failed: %v", err)
	}
	defer onDisconnectDrivers.Close()

	Logger.Debugf("Byte written %v", stats.WrittenBytes)
	return result.err
}
示例#23
0
func main() {
	// store information about this fake process into JSON
	signal.Ignore()

	// validate command line arguments
	// expect them to look like
	//   fake-thing agent -config-dir=/some/path/to/some/dir
	if len(os.Args[1:]) == 0 {
		log.Fatal("expecting command as first argment")
	}

	var configDir string
	var recursors stringSlice
	flagSet := flag.NewFlagSet("", flag.ExitOnError)
	flagSet.StringVar(&configDir, "config-dir", "", "config directory")
	flagSet.Var(&recursors, "recursor", "recursor")
	flagSet.Parse(os.Args[2:])
	if configDir == "" {
		log.Fatal("missing required config-dir flag")
	}

	ow := NewOutputWriter(filepath.Join(configDir, "fake-output.json"), os.Getpid(), os.Args[1:])

	// read input options provided to us by the test
	var inputOptions struct {
		Members           []string
		FailRPCServer     bool
		FailStatsEndpoint bool
	}

	if optionsBytes, err := ioutil.ReadFile(filepath.Join(configDir, "options.json")); err == nil {
		json.Unmarshal(optionsBytes, &inputOptions)
	}

	tcpAddr := ""
	if !inputOptions.FailRPCServer {
		tcpAddr = "127.0.0.1:8400"
	}

	server := &Server{
		HTTPAddr:          "127.0.0.1:8500",
		TCPAddr:           tcpAddr,
		Members:           inputOptions.Members,
		OutputWriter:      ow,
		FailStatsEndpoint: inputOptions.FailStatsEndpoint,
	}

	err := server.Serve()
	if err != nil {
		log.Fatalf("Failed to start server: %s\n", err)
	}

	for {
		if server.DidLeave {
			err := server.Exit()
			if err != nil {
				log.Fatalf("Failed to close server: %s\n", err)
			}

			ow.Exit()
			os.Exit(0)
		}

		time.Sleep(100 * time.Millisecond)
	}
}
示例#24
0
文件: signal.go 项目: Xiahl1990/go
func SignalIgnoreSIGTRAP() {
	signal.Ignore(syscall.SIGTRAP)
	syscall.Kill(syscall.Getpid(), syscall.SIGTRAP)
	println("OK")
}