Example #1
0
func main() {
	var (
		debug       bool
		justVersion bool
		c           = proxy.Config{ListenAddr: defaultListenAddr}
	)

	c.Version = version
	getopt.BoolVarLong(&debug, "debug", 'd', "log debugging information")
	getopt.BoolVarLong(&justVersion, "version", 0, "print version and exit")
	getopt.StringVar(&c.ListenAddr, 'H', fmt.Sprintf("address on which to listen (default %s)", defaultListenAddr))
	getopt.BoolVarLong(&c.NoDefaultIPAM, "no-default-ipam", 0, "do not automatically allocate addresses for containers without a WEAVE_CIDR")
	getopt.StringVarLong(&c.TLSConfig.CACert, "tlscacert", 0, "Trust certs signed only by this CA")
	getopt.StringVarLong(&c.TLSConfig.Cert, "tlscert", 0, "Path to TLS certificate file")
	getopt.BoolVarLong(&c.TLSConfig.Enabled, "tls", 0, "Use TLS; implied by --tlsverify")
	getopt.StringVarLong(&c.TLSConfig.Key, "tlskey", 0, "Path to TLS key file")
	getopt.BoolVarLong(&c.TLSConfig.Verify, "tlsverify", 0, "Use TLS and verify the remote")
	getopt.BoolVarLong(&c.WithDNS, "with-dns", 'w', "instruct created containers to always use weaveDNS as their nameserver")
	getopt.BoolVarLong(&c.WithoutDNS, "without-dns", 0, "instruct created containers to never use weaveDNS as their nameserver")
	getopt.Parse()

	if justVersion {
		fmt.Printf("weave proxy %s\n", version)
		os.Exit(0)
	}

	if c.WithDNS && c.WithoutDNS {
		Error.Fatalf("Cannot use both '--with-dns' and '--without-dns' flags")
	}

	if debug {
		InitDefaultLogging(true)
	}

	Info.Println("weave proxy", version)
	Info.Println("Command line arguments:", strings.Join(os.Args[1:], " "))

	protoAddrParts := strings.SplitN(c.ListenAddr, "://", 2)
	if len(protoAddrParts) == 2 {
		if protoAddrParts[0] != "tcp" {
			Error.Fatalf("Invalid protocol format: %q", protoAddrParts[0])
		}
		c.ListenAddr = protoAddrParts[1]
	} else {
		c.ListenAddr = protoAddrParts[0]
	}

	p, err := proxy.NewProxy(c)
	if err != nil {
		Error.Fatalf("Could not start proxy: %s", err)
	}

	if err := p.ListenAndServe(); err != nil {
		Error.Fatalf("Could not listen on %s: %s", p.ListenAddr, err)
	}
}
Example #2
0
func main() {
	log.SetFlags(log.LstdFlags | log.Lshortfile)
	// var needle string
	// var filepattern string
	var distance int
	var patternFile string
	var help bool
	var verbose bool
	var simpleoutput bool

	getopt.IntVarLong(&distance, "edit", 'e', "Compute the approximate matching", "max_dist")
	getopt.StringVarLong(&patternFile, "pattern", 'p', "Use line-break separated patterns from a file", "filepath")
	getopt.StringVarLong(&cpuprofile, "cpuprofile", 0, "Write cpuprofile file", "path")
	getopt.StringVarLong(&memprofile, "memprofile", 0, "Write memprofile file", "path")
	getopt.BoolVarLong(&help, "help", 'h', "Shows this message")
	getopt.BoolVarLong(&verbose, "verbose", 'v', "Show log messages")
	getopt.BoolVarLong(&simpleoutput, "simple", 's', "Show simple output")
	getopt.SetProgram("pmt")
	getopt.SetParameters("needle [haystack ...]")
	getopt.SetUsage(func() {
		getopt.PrintUsage(os.Stderr)
		fmt.Fprintf(os.Stderr, "needle - only if -p was not used\n")
		fmt.Fprint(os.Stderr, "haystack\n")
	})
	getopt.Parse()

	if cpuprofile != "" {
		f, err := os.Create(cpuprofile)
		if err != nil {
			log.Fatal(err)
		}
		pprof.StartCPUProfile(f)
		defer pprof.StopCPUProfile()
	}

	if help {
		getopt.Usage()
		return
	}

	var patterns []string
	var files []string

	if patternFile == "" {
		// if(len(getopt.Args() )
		if getopt.NArgs() < 1 {
			fmt.Fprintf(os.Stderr, "Needle is missing!\n")
			getopt.Usage()
			os.Exit(1)
		}
		patterns = getopt.Args()[:1]
		files = getopt.Args()[1:]

	} else {
		var err error
		patterns, err = readLinesFromFile(patternFile)
		if err != nil {
			log.Fatal(err)
		}
		files = getopt.Args()
	}

	if verbose {
		log.Printf("%v %v %v\n", patterns, files, distance)
	}

	fileset := findFilesMatch(files)

	if distance == 0 {
		if len(patterns) == 1 {
			matcher := streammatch.NewKMP([]byte(patterns[0]))
			for fp, _ := range fileset {
				file, err := os.Open(fp)
				if err != nil {
					log.Fatal(err)
				}
				matches, err := processSingleExactMatcher(file, matcher, true)

				if err != nil {
					log.Fatal(err)
				}

				if !simpleoutput {
					printMatches(fp, file, patterns, matches, distance)
					if len(matches) > 0 {
						fmt.Println("###")
					}
				} else {
					printSimpleMatches(fp, file, patterns, matches)
				}

			}
		} else if len(patterns) > 1 {
			bpatterns := make([][]byte, len(patterns))
			for i, pattern := range patterns {
				bpatterns[i] = []byte(pattern)
			}
			matcher := streammatch.NewAhoCorasick(bpatterns)
			for fp, _ := range fileset {
				file, err := os.Open(fp)
				if err != nil {
					log.Fatal(err)
				}
				matches, err := processMultiExactMatcher(file, matcher)

				if err != nil {
					log.Fatal(err)
				}

				if !simpleoutput {
					printMatches(fp, file, patterns, matches, distance)
					if len(matches) > 0 {
						fmt.Println("###")
					}
				} else {
					printSimpleMatches(fp, file, patterns, matches)
				}

			}
		}
	} else {
		matchers := make([]streammatch.Matcher, 0, len(patterns))
		for i := 0; i < len(patterns); i++ {
			matchers = append(matchers, streammatch.NewSellers([]byte(patterns[i]), distance))
		}

		for fp, _ := range fileset {
			file, err := os.Open(fp)
			if err != nil {
				log.Fatal(err)
			}

			bufreader := bufio.NewReaderSize(file, defaultBufSize)
			allmatches := make([]matchRecord, 0, 2)
			for _, matcher := range matchers {
				_, err := file.Seek(0, 0)
				if err != nil {
					log.Fatal(err)
				}
				bufreader.Reset(file)
				matches, err := processSingleExactMatcher(bufreader, matcher, false)
				if err != nil {
					log.Fatal(err)
				}
				allmatches = append(allmatches, matches...)
			}

			sort.Stable(matchRecordList(allmatches))

			if !simpleoutput {
				printMatches(fp, file, patterns, allmatches, distance)
				if len(allmatches) > 0 {
					fmt.Println("###")
				}
			} else {
				printSimpleMatches(fp, file, patterns, allmatches)
			}
		}
	}
	if memprofile != "" {
		f, err := os.Create(memprofile)
		if err != nil {
			log.Fatal(err)
		}
		pprof.WriteHeapProfile(f)
		defer f.Close()
		return
	}
}