示例#1
0
文件: scan.go 项目: XiliangSong/gscan
func test_conn(conn *tls.Conn, options *ScanOptions, record *ScanRecord) bool {
	//check SSL certificate
	success := false
	for _, cert := range conn.ConnectionState().PeerCertificates {
		for _, verifyHost := range options.Config.ScanGoogleIP.SSLCertVerifyHosts {
			if cert.VerifyHostname(verifyHost) != nil {
				return false
			} else {
				success = true
			}
		}
		if success {
			break
		}
	}
	for _, verifyHost := range options.Config.ScanGoogleIP.HTTPVerifyHosts {
		conn.SetReadDeadline(time.Now().Add(record.httpVerifyTimeout))
		req, _ := http.NewRequest("HEAD", "https://"+verifyHost, nil)
		res, err := httputil.NewClientConn(conn, nil).Do(req)
		if nil != err || res.StatusCode >= 400 {
			return false
		}
	}
	return true
}
示例#2
0
文件: scan.go 项目: XiliangSong/gscan
func find_match_hosts(conn *tls.Conn, options *ScanOptions, record *ScanRecord) bool {
	if nil == record.MatchHosts || len(record.MatchHosts) == 0 {
		record.MatchHosts = options.SSLMatchHosts(conn)
	}

	newhosts := make([]string, 0)
	for _, host := range record.MatchHosts {
		if options.HaveHostInRecords(host) {
			continue
		}
		newhosts = append(newhosts, host)
		valid := true
		for _, pattern := range options.Config.ScanGoogleHosts.HTTPVerifyHosts {
			if matchHostnames(pattern, host) {
				conn.SetReadDeadline(time.Now().Add(record.httpVerifyTimeout))
				req, _ := http.NewRequest("HEAD", "https://"+host, nil)
				res, err := httputil.NewClientConn(conn, nil).Do(req)
				if nil != err || res.StatusCode >= 400 {
					valid = false
				}
				break
			}
		}
		if valid {
			newhosts = append(newhosts, host)
		}
	}
	record.MatchHosts = newhosts
	return len(record.MatchHosts) > 0
}
示例#3
0
func serverFunc(conn *tls.Conn) {
	defer conn.Close()
	conn.SetReadDeadline(time.Now().Add(time.Second))
	io.Copy(conn, conn)
}
示例#4
0
文件: irc.go 项目: heydabop/yaircb
func main() {
	startTime = time.Now()
	runtime.GOMAXPROCS(4)
	rand.Seed(time.Now().Unix())
	//read in bot config, or initialize default config
	configFile, err := ioutil.ReadFile("config.json")
	if err == nil {
		err = json.Unmarshal(configFile, &config)
		if err != nil {
			log.Fatal("Error unmarshalling config.json")
		}
	} else {
		config = JSONconfig{"chat.freenode.net", 6667, "yaircb", "", "*", false, make([]string, 0), make([]string, 0)}
	}
	fmt.Println(config)

	//set up command detection regular expressions
	regexpCmds = make([]*regexp.Regexp, 3)
	regexpCmds[0] = regexp.MustCompile(`^:(\S*?)!(\S*?)@(\S*?) PRIVMSG (\S*) :` + config.Nick + `\W?\s*(.*)`)
	regexpCmds[1] = regexp.MustCompile(`^:(\S*)?!(\S*)?@(\S*)? PRIVMSG (\S*) :\s*\+(.*)`)
	regexpCmds[2] = regexp.MustCompile(`^:(\S*)?!(\S*)?@(\S*)? PRIVMSG (` + config.Nick + `) :\s*(.*)`)

	//initialize global string->function command map
	funcMap = initMap()
	err = initCmdRedis()
	if err != nil { //if redis init fails, print error
		log.Println(err)
	}

	if err == nil { //only run web server is redis init doesn't fail
		//initialize web server
		initWebRedis()
		http.Handle("/resources/", http.StripPrefix("/resources/", http.FileServer(http.Dir("resources"))))
		http.HandleFunc("/register/", registerHandler)
		http.HandleFunc("/login/", loginHandler)
		http.HandleFunc("/loginCheck/", loginCheckHandler)
		http.HandleFunc("/", indexHandler)
		http.HandleFunc("/save/", saveHandler)
		http.HandleFunc("/user/", userHandler)
		go http.ListenAndServeTLS(":8080", "ssl.crt", "ssl.pem", nil)
	}

	var conns uint16
	writeChan := make(chan string) //used to send strings from readFromConsole to writeToServer
	readChan := make(chan string)  //send strings from readFromServer to writeToConsole
	//wgSrv for goroutines to/from sever, wg for readFromConsole
	var wgSrv, wg sync.WaitGroup
	quitChans := make(chan chan bool, 2)
	error := make(chan bool, 1) //used to indicate readFromConsole exited
	//initiate connection
	wg.Add(2)
	go readFromConsole(writeChan, &wg, error, quitChans) //doesnt get restarted on connection EOF
	wtsQChan := make(chan bool, 1)
	go writeToConsole(readChan, writeChan, &wg, wtsQChan, quitChans) //doesnt get restarted on connection EOF
connectionLoop:
	for ; ; conns++ {
		select {
		case <-error: //if readFromConsole got a "QUIT", exit program
			wtsQChan <- true
			break connectionLoop
		default: //otherwise restart connections
			if conns == 0 {
				log.Println("STARTING...")
			} else {
				log.Println("RESTARTING...")
				log.Println("WAITING 1 MINUTE...")
				time.Sleep(time.Minute)
			}
			var socketRead *bufio.Reader
			var socketWrite *bufio.Writer
			err := errors.New("")
			if config.TLS {
				log.Printf("Connecting to %s:%d with TLS...\n", config.Server, config.Port)
				var sslSocket *tls.Conn
				sslSocket, err = tls.Dial("tcp", fmt.Sprintf("%s:%d", config.Server, config.Port), nil)
				if err == nil {
					sslSocket.SetReadDeadline(time.Time{})
					socketWrite = bufio.NewWriter(sslSocket)
					socketRead = bufio.NewReader(sslSocket)
				} else {
					log.Println("Disabling TLS...")
				}
			}
			if err != nil || !config.TLS { //!config.TLS shouldn't actually be evaluted, as err != nil would be true from err init
				log.Printf("Connecting to %s:%d...\n", config.Server, config.Port)
				socket, err := textproto.Dial("tcp", fmt.Sprintf("%s:%d", config.Server, config.Port))
				if err != nil {
					errOut(err, quitChans)
					return
				}
				socketWrite = socket.Writer.W
				socketRead = socket.Reader.R
			}
			//make writer/reader to/from server
			//send initial IRC messages, NICK and USER
			_, err = socketWrite.WriteString("NICK " + config.Nick + "\r\n")
			if err == nil {
				err = socketWrite.Flush()
			}
			log.Print("NICK " + config.Nick + "\r\n")
			if err != nil {
				errOut(err, quitChans)
			}
			_, err = socketWrite.WriteString("USER " + config.Nick + " " + config.Hostname + " * :yaircb\r\n")
			if err == nil {
				err = socketWrite.Flush()
			}
			log.Print("USER " + config.Nick + " " + config.Hostname + " * :yaircb\r\n")
			if err != nil {
				errOut(err, quitChans)
			}
			wgSrv.Add(1)
			//launch routine to write server output to console
			rfsQChan := make(chan bool, 1)
			quitChans <- rfsQChan
			go readFromServer(socketRead, readChan, &wgSrv, rfsQChan, quitChans)
			//join first channel
			/*err = socket.Writer.PrintfLine("JOIN #ttestt")
			if err != nil {
				errOut(err, quit)
			}*/
			if config.NickServPass != "" {
				_, err = socketWrite.WriteString("PRIVMSG NickServ :IDENTIFY " + config.NickServPass + "\r\n")
				if err == nil {
					err = socketWrite.Flush()
				}
				if err != nil {
					errOut(err, quitChans)
				}
			}
			wgSrv.Add(1)
			//launch routine to send to server and get input from console
			wtsQChan := make(chan bool, 1)
			quitChans <- wtsQChan
			go writeToServer(socketWrite, writeChan, &wgSrv, wtsQChan, quitChans)
			if len(config.Channels) > 0 { //join supplied channels upon connection
				joinMsg := "JOIN "
				for _, channel := range config.Channels {
					joinMsg += channel + ","
				}
				log.Println(joinMsg[:len(joinMsg)-1])
				writeChan <- joinMsg[:len(joinMsg)-1]
			}
			wgSrv.Wait()
		}
	}
	wg.Wait()
}