Example #1
0
func (s *Server) sendRA(src net.Addr) {
	var srcIP net.IP
	var ipAddr net.Addr

	iface, err := net.InterfaceByName("tap" + s.name)
	if err != nil {
		return
	}
	addrs, err := iface.Addrs()
	if err != nil {
		l.Info("can't get addresses from " + iface.Name + " " + err.Error())
		return
	}
	for _, addr := range addrs {
		ip, _, err := net.ParseCIDR(addr.String())
		if err != nil {
			l.Info(err.Error())
			continue
		}
		if ip.To4() == nil && strings.HasPrefix(addr.String(), "fe80") {
			srcIP = ip
			if src == nil {
				ipAddr = net.Addr(&net.IPAddr{IP: net.IPv6linklocalallnodes, Zone: "tap" + s.name})
			} else {
				ipAddr = src
			}
			break
		}
	}

	if ipAddr == nil || srcIP == nil {
		l.Info(fmt.Sprintf("ipv6 add missing for tap %s %s", s.name, srcIP))
		return
	}
	res, err := s.ServeICMPv6(srcIP, &ICMPv6{Type: uint8(ipv6.ICMPTypeRouterSolicitation)})
	if err != nil {
		l.Info(err.Error())
		return
	}

	for _, msg := range res {
		buf := make([]byte, msg.Len())
		buf, err = msg.Marshal()
		if err != nil {
			l.Info(fmt.Sprintf("%s err %s", s.name, err.Error()))
			continue
		}

		wcm := ipv6.ControlMessage{HopLimit: 255}
		wcm.Dst = net.IPv6linklocalallnodes
		wcm.IfIndex = iface.Index
		if _, err = s.ipv6conn.WriteTo(buf, &wcm, ipAddr); err != nil {
			l.Info(fmt.Sprintf("%s err %s", s.name, err.Error()))
			continue
		}
	}
}
Example #2
0
func Dial(network, address string) (c *Conn, err error) {
	i := strings.LastIndex(network, ":")
	if i > 0 { // has colon
		return nil, &errorString{"Not supported yet"} // TODO
	}

	ra, err := net.ResolveUDPAddr(network, address)
	if err != nil {
		return nil, err
	}

	return dialQuic(network, net.Addr(ra).(*net.UDPAddr))
}
Example #3
0
func (s *testServer) UnaryCall(ctx context.Context, in *testpb.SimpleRequest) (*testpb.SimpleResponse, error) {
	md, ok := metadata.FromContext(ctx)
	if ok {
		if err := grpc.SendHeader(ctx, md); err != nil {
			return nil, fmt.Errorf("grpc.SendHeader(%v, %v) = %v, want %v", ctx, md, err, nil)
		}
		grpc.SetTrailer(ctx, md)
	}
	pr, ok := peer.FromContext(ctx)
	if !ok {
		return nil, fmt.Errorf("failed to get peer from ctx")
	}
	if pr.Addr == net.Addr(nil) {
		return nil, fmt.Errorf("failed to get peer address")
	}
	if s.security != "" {
		// Check Auth info
		var authType, serverName string
		switch info := pr.AuthInfo.(type) {
		case credentials.TLSInfo:
			authType = info.AuthType()
			serverName = info.State.ServerName
		default:
			return nil, fmt.Errorf("Unknown AuthInfo type")
		}
		if authType != s.security {
			return nil, fmt.Errorf("Wrong auth type: got %q, want %q", authType, s.security)
		}
		if serverName != "x.test.youtube.com" {
			return nil, fmt.Errorf("Unknown server name %q", serverName)
		}
	}

	// Simulate some service delay.
	time.Sleep(time.Second)

	payload, err := newPayload(in.GetResponseType(), in.GetResponseSize())
	if err != nil {
		return nil, err
	}

	return &testpb.SimpleResponse{
		Payload: payload,
	}, nil
}
Example #4
0
File: 1.go Project: dlsniper/u
func main() {
	a := net.Addr()
	_ = a
}
Example #5
0
func (session *UDPSession) RemoteAddr() net.Addr {
	return net.Addr(session.remote)
}
Example #6
0
func TestHooks(t *testing.T) {

	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if r.URL.String() == "/bad" {
			w.WriteHeader(500)
			fmt.Fprintln(w, "bad response")
		}

		if r.URL.String() == "/test/web.txt" {
			w.WriteHeader(200)
			fmt.Fprintln(w, "RES:web.txt")
			return
		}

	}))
	defer ts.Close()

	clientAddr := net.Addr(&net.TCPAddr{
		IP:   net.ParseIP("198.51.100.13"),
		Port: 63233,
	})

	var hookTestCases = []hookTestCase{
		{
			&config.HookDef{
				Type:     "file",
				Regexp:   ".*",
				Template: "file_fixture.txt",
			},
			"anything",
			"filecontents",
			noError,
		},
		{
			&config.HookDef{
				Type:     "file",
				Regexp:   ".*",
				Template: "$0",
			},
			"../file_fixture.txt",
			"filecontents",
			noError,
		},
		{
			&config.HookDef{
				Type:     "file",
				Regexp:   "^extension:(.*)$",
				Template: "file_fixture.$1",
			},
			"extension:txt",
			"filecontents",
			noError,
		},
		{
			&config.HookDef{
				Type:     "shell",
				Regexp:   ".*",
				Template: "echo shellhello",
			},
			"anything",
			"shellhello",
			noError,
		},
		{
			&config.HookDef{
				Type:     "shell",
				Regexp:   "foo:(.*)",
				Template: "echo $1",
			},
			"foo:haha",
			"haha",
			noError,
		},
		{
			&config.HookDef{
				Type:     "shell",
				Regexp:   "foo:(.*)",
				Template: "echo $1",
			},
			"foo:$(hostname)",
			"$(hostname)",
			noError,
		},
		{
			&config.HookDef{
				Type:     "shell",
				Regexp:   "foo:(.*)",
				Template: "echo $1",
			},
			"foo:`hostname`",
			"`hostname`",
			noError,
		},
		{
			&config.HookDef{
				Type:     "shell",
				Regexp:   "foo:(.*)",
				Template: "echo $1",
			},
			"foo:$HOME",
			"$HOME",
			noError,
		},
		{
			&config.HookDef{
				Type:     "shell",
				Regexp:   "(.*)",
				Template: "echo $1; exit 1",
			},
			"foo",
			"foo",
			noError,
		},
		{
			&config.HookDef{
				Type:     "shell",
				Regexp:   ".*",
				Template: "sh -c 'echo $CLIENT_ADDR'",
			},
			"anything",
			clientAddr.String(),
			noError,
		},
		{
			&config.HookDef{
				Type:     "shell",
				Regexp:   ".*",
				Template: "",
			},
			"anything",
			"anything",
			func(err error) error {
				if err == nil {
					return fmt.Errorf("Bad url response test failed: Expected to have an error")
				}
				return nil
			},
		},
		{
			&config.HookDef{
				Type:     "http",
				Regexp:   "url\\/(.+)$",
				Template: ts.URL + "/test/$1",
			},
			"url/web.txt",
			"RES:web.txt",
			noError,
		},
		{
			&config.HookDef{
				Type:     "http",
				Regexp:   "url\\/(.+)$",
				Template: ts.URL + "/bad",
			},
			"url/bad.txt",
			"bad response",
			func(err error) error {
				if err == nil {
					return fmt.Errorf("Bad url response test failed: Expected to have an error")
				}
				return nil
			},
		},
	}

	for _, testCase := range hookTestCases {
		hook, err := CompileHook(testCase.hookDef)
		if err != nil {
			t.Error("Failed to compile", testCase.hookDef, err)
			return
		}

		fakeRequest := tftp.Request{Addr: &clientAddr}

		hookResult, err := hook(testCase.input, fakeRequest)
		if err == NO_MATCH {
			t.Error(testCase.hookDef.Regexp, "does not match with", testCase.input)
		}

		if err = testCase.errorValidator(err); err != nil {
			t.Error("Failed to execute hook:", testCase.hookDef, err)
			return
		}

		if hookResult == nil {
			return
		}

		data, err := ioutil.ReadAll(hookResult.Stdout)
		if err != nil {
			t.Error("Failed to read file", testCase.hookDef, hookResult.Stdout)
			return
		}

		res := string(data[:len(testCase.expected)])

		if res != testCase.expected {
			t.Errorf("Expected to find '%v' from file but got '%v'", testCase.expected, res)
		}
	}
}