func single_stew_echo_client() {
	ctx := make_sc_ctx()
	sc1, err := net.Dial("tcp", "127.0.0.1:20999")
	if err != nil {
		panic(err.Error())
	}

	go ctx.AttachSC(sc1, false)
	sc2, err := net.Dial("tcp", "127.0.0.1:20999")
	if err != nil {
		panic(err.Error())
	}
	go ctx.AttachSC(sc2, false)
	kilog.Info("Two wires attached!")
	kilog.Info("Sending hello world...")
	ctx.write_ch <- sc_message{0x01, []byte(" ")}
	close(<-ctx.close_ch_ch)
	ctx.write_ch <- sc_message{0x00, []byte("hello")}
	sc3, err := net.Dial("tcp", "127.0.0.1:20999")
	if err != nil {
		panic(err.Error())
	}
	go ctx.AttachSC(sc3, false)
	ctx.write_ch <- sc_message{0x02, []byte("world")}
	close(<-ctx.close_ch_ch)
	fmt.Print(string((<-ctx.ordered_ch).payload))
	fmt.Print(string((<-ctx.ordered_ch).payload))
	fmt.Println(string((<-ctx.ordered_ch).payload))
}
Exemplo n.º 2
0
func runDir() {
	fh, err := os.Open("_SECRETS_/privkey-sanctum.bin")
	defer fh.Close()
	if err != nil {
		panic(err.Error())
	}
	masterPriv := natrium.EdDSAGenerateKey()
	_, err = io.ReadFull(fh, masterPriv)
	if err != nil {
		panic(err.Error())
	}
	haha := directory.NewServer(masterPriv)

	kilog.Info("starting SAGIRI directory on port 2378")
	server, err := net.Listen("tcp", "0.0.0.0:2378")
	if err != nil {
		panic(err.Error())
	}

	for {
		rawcl, err := server.Accept()
		if err != nil {
			return
		}
		go func() {
			defer rawcl.Close()
			haha.HandleClient(rawcl)
		}()
	}
}
Exemplo n.º 3
0
func initialize() {
	kilog.Info("SAGIRI version %v initializing...", VERSION)
	flag.Parse()
	lol, err := getPublicIP()
	kilog.Info("%v %v", lol, err)

	if isDirectory {
		runDir()
		os.Exit(0)
	}

	switch dirHelper {
	case "internal":
		dirProvider = directory.DefaultProvider("sanctum.sagiri.niwl.io:2378")
	}

	// obtain directory info
	id := directory.NewClient(natrium.EdDSAGenerateKey())
	kilog.Debug("master key: %v", id.GetPrivate().PublicKey())
	getNeighs(id)

	go func() {
		for {
			time.Sleep(time.Minute)
			getNeighs(id)
		}
	}()

	// set up core node
	if onionPort != 0 {
		ip, err := getPublicIP()
		if err != nil {
			panic(err.Error())
		}
		id.SetAddress(&net.TCPAddr{IP: ip, Port: onionPort, Zone: "tcp"})
		dirClient = id
		go runCore(id)
	} else {
		dirClient = id
		startSocks()
		startControl()
		go func() {
			log.Println(http.ListenAndServe("0.0.0.0:6060", nil))
		}()
	}
	kilog.Info("SAGIRI finished initializing.")
}
Exemplo n.º 4
0
func main() {
	kilog.Info("Starting megaproxy server...")
	http.HandleFunc("/dl/", proxhandler)
	http.HandleFunc("/", urlgen)
	err := http.ListenAndServe("127.0.0.1:18081", nil)
	if err != nil {
		panic(err.Error())
	}
}
Exemplo n.º 5
0
func startSocks() {
	listener, err := net.Listen("tcp", fmt.Sprintf("0.0.0.0:%v", socksPort))
	if err != nil {
		kilog.Critical("failed to bind SOCKS5 listener: %v", err.Error())
		os.Exit(-1)
	}
	go func() {
		for {
			clnt, err := listener.Accept()
			if err != nil {
				continue
			}
			go handleSocks(clnt)
		}
	}()
	kilog.Info("SOCKS5 listener successfully bound at %v", listener.Addr())
}
Exemplo n.º 6
0
func runCore(id directory.Client) {
	state := core2core.NewNodeState(dirProvider, id)

	addr := id.GetAddress()
	srv, err := kiricom.EasyListen(fmt.Sprintf("0.0.0.0:%v", addr.Port), nil, id.GetSecret())
	if err != nil {
		panic(err.Error())
	}

	err = dirProvider.JoinCore(id)
	if err != nil {
		panic(err.Error())
	}

	go func() {
		for {
			time.Sleep(time.Minute * 5)
			err = dirProvider.JoinCore(id)
			if err != nil {
				kilog.Warning("core: error while joining %v", err.Error())
			}
		}
	}()

	fmt.Println(id.GetAddress())

	kilog.Info("started core service on %v", addr)
	for {
		client, err := srv.Accept()
		if err != nil {
			panic(err.Error())
		}
		kilog.Debug("core: accepted a client")
		go func() {
			err := state.HandleClient(client)
			if err != nil {
				kilog.Warning("core: error %v", err)
			}
		}()
	}
}
func run_client_loop() {
	listener, err := net.Listen("tcp", MasterConfig.General.SocksAddr)
	if err != nil {
		panic(err)
	}
	circ_ch <- produce_circ()
	set_gui_progress(1.0)
	kilog.Info("Bootstrapping 100%%: client started!")

	go func() {
		var haha sync.WaitGroup
		haha.Add(5)
		for i := 0; i < 5; i++ {
			go func() {
				circ_ch <- produce_circ()
				haha.Done()
			}()
		}
		haha.Wait()
	}()
	for {
		nconn, err := listener.Accept()
		if err != nil {
			kilog.Warning("Problem while accepting client socket: %s", err.Error())
			continue
		}
		go func() {
			defer func() {
				nconn.Close()
			}()
			addr, err := socks5.ReadRequest(nconn)
			if err != nil {
				kilog.Warning("Problem while reading SOCKS5 request")
				return
			}
			kilog.Debug("Attempting connection to %s...", addr)
		retry:
			newcirc := <-circ_ch
			remote, err := newcirc.SocksAccept(nconn)
			if err != nil {
				dirclient.RefreshDirectory()
				circ_ch <- produce_circ()
				goto retry
			}
			circ_ch <- newcirc
			defer remote.Close()
			lenbts := []byte{byte((len(addr) + 1) % 256), byte((len(addr) + 1) / 256)}
			_, err = remote.Write(lenbts)
			_, err = remote.Write([]byte(fmt.Sprintf("t%s", addr)))
			if err != nil {
				kilog.Debug("Failed to send tunnelling request to %s!", addr)
				socks5.CompleteRequest(0x03, nconn)
				return
			}

			kilog.Debug("Sent tunneling request")

			code := make([]byte, 4)
			_, err = io.ReadFull(remote, code)
			if err != nil {
				kilog.Debug("Failed to read response for %s! (%s)", addr, err)
				socks5.CompleteRequest(0x03, nconn)
				return
			}

			switch string(code) {
			case "OKAY":
				kilog.Debug("Successfully tunneled %s!", addr)
				socks5.CompleteRequest(0x00, nconn)
				go func() {
					defer remote.Close()
					io.Copy(remote, nconn)
				}()
				io.Copy(nconn, remote)
			case "TMOT":
				kilog.Debug("Tunnel to %s timed out!", addr)
				socks5.CompleteRequest(0x06, nconn)
			case "NOIM":
				kilog.Debug("Tunnel type for %s isn't implemented by server!", addr)
				socks5.CompleteRequest(0x07, nconn)
			case "FAIL":
				kilog.Debug("Tunnel to %s cannot be established!", addr)
				socks5.CompleteRequest(0x04, nconn)
			default:
				kilog.Debug("Protocol error on tunnel to %s! (%s)", addr, string(code))
				socks5.CompleteRequest(0x01, nconn)
				return
			}
		}()
	}
}
Exemplo n.º 8
0
func main() {
	rand.Seed(time.Now().UnixNano())
	go run_monitor_loop()
	flag.Parse()
	if *confloc == "" {
		kilog.Warning("No configuration file given, using defaults")
	} else {
		err := gcfg.ReadFileInto(&MasterConfig, *confloc)
		if err != nil {
			kilog.Warning("Configuration file broken, using defaults")
		}
	}
	if *singhop {
		MasterConfig.Network.MinCircuitLen = 1
	}
	kilog.Info("Kirisurf %s started! mkh=%s", version, MasterKeyHash)
	set_gui_progress(0.1)
	kilog.Info("Bootstrapping 10%%: finding directory address...")
	dirclient.DIRADDR, _ = dirclient.FindDirectoryURL()
	set_gui_progress(0.2)
	kilog.Info("Bootstrapping 20%%: found directory address, refreshing directory...")
	err := dirclient.RefreshDirectory()
	if err != nil {
		kilog.Critical("Stuck at 20%%: directory connection error %s", err.Error())
		for {
			time.Sleep(time.Second)
		}
	}
	set_gui_progress(0.3)
	kilog.Info("Bootstrapping 30%%: directory refreshed, beginning to build circuits...")
	go run_diagnostic_loop()
	dirclient.RefreshDirectory()
	if MasterConfig.General.Role == "server" {
		NewSCServer(MasterConfig.General.ORAddr)
		addr := RegisterNGSCServer(MasterConfig.General.ORAddr)
		prt, _ := strconv.Atoi(
			strings.Split(MasterConfig.General.ORAddr, ":")[1])
		go func() {
			err := UPnPForwardAddr(MasterConfig.General.ORAddr)
			err = UPnPForwardAddr(addr)
			if err != nil {
				kilog.Warning("UPnP failed: %s", err)
				if MasterConfig.Network.OverrideUPnP {
					go dirclient.RunRelay(prt, MasterKeyHash,
						MasterConfig.General.IsExit)
				}
				return
			}
			kilog.Info("UPnP successfully forwarded port")
			go dirclient.RunRelay(prt, MasterKeyHash,
				MasterConfig.General.IsExit)
		}()
		kilog.Info("Started server!")
		if *noclient {
			for {
				time.Sleep(time.Second)
			}
		}
	}
	run_client_loop()
	kilog.Info("Kirisurf exited")
}
Exemplo n.º 9
0
func main() {
	kilog.Info("DANASI 0.1 starting...")
	var _dnsThis string
	var _dnsNext string
	flag.StringVar(&_dnsThis, "dnsThis", "127.0.0.1:53535", "address on which to listen for DNS")
	flag.StringVar(&_dnsNext, "dnsNext", "8.8.8.8:53", "address to which unrelated requests are forwarded")

	var _transThis string
	var _socksNext string
	flag.StringVar(&_transThis, "transThis", "127.0.0.1:12377", "address to which TCP connections in the IP range should be forwarded")
	flag.StringVar(&_socksNext, "socksNext", "127.0.0.1:2377", "SOCKS5 proxy with which matching names are dialed upon")

	var _ipRange string
	flag.StringVar(&_ipRange, "ipRange", "100.64.0.0/10", "IP range from which addresses are allocated")

	var _namePattern string
	flag.StringVar(&_namePattern, "namePattern", ".*", "regexp (POSIX) matching DNS names to be intercepted")
	flag.Parse()
	kilog.Debug("flags read:")
	kilog.Debug("    dnsThis = %v", _dnsThis)
	kilog.Debug("    dnsNext = %v", _dnsNext)
	kilog.Debug("    transThis = %v", _transThis)
	kilog.Debug("    socksNext = %v", _socksNext)
	kilog.Debug("    ipRange = %v", _ipRange)
	kilog.Debug("    namePattern = %v", _namePattern)

	var err error

	// validate the command-line arguments
	dnsThis, err = net.ResolveUDPAddr("udp", _dnsThis)
	if err != nil {
		kilog.Critical("*** malformed dnsThis: %v ***", err.Error())
		os.Exit(-1)
	}
	dnsNext, err = net.ResolveUDPAddr("udp", _dnsNext)
	if err != nil {
		kilog.Critical("*** malformed dnsNext: %v ***", err.Error())
		os.Exit(-1)
	}

	transThis, err = net.ResolveTCPAddr("tcp", _transThis)
	if err != nil {
		kilog.Critical("*** malformed transThis: %v ***", err.Error())
		os.Exit(-1)
	}
	socksNext, err = net.ResolveTCPAddr("tcp", _socksNext)
	if err != nil {
		kilog.Critical("*** malformed socksNext: %v ***", err.Error())
		os.Exit(-1)
	}

	_, ipRange, err = net.ParseCIDR(_ipRange)
	if err != nil {
		kilog.Critical("*** malformed ipRange: %v ***", err.Error())
		os.Exit(-1)
	}

	namePattern, err = regexp.CompilePOSIX(_namePattern)
	if err != nil {
		kilog.Critical("*** malformed namePattern: %v ***", err.Error())
		os.Exit(-1)
	}

	go dnsDispatch()
	tcpLoop()
}