1/*
2 *
3 * Copyright 2014 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19package main
20
21import (
22	"flag"
23	"net"
24	"strconv"
25
26	"google.golang.org/grpc"
27	"google.golang.org/grpc/credentials"
28	"google.golang.org/grpc/credentials/alts"
29	"google.golang.org/grpc/grpclog"
30	"google.golang.org/grpc/interop"
31	testpb "google.golang.org/grpc/interop/grpc_testing"
32	"google.golang.org/grpc/testdata"
33)
34
35var (
36	useTLS     = flag.Bool("use_tls", false, "Connection uses TLS if true, else plain TCP")
37	useALTS    = flag.Bool("use_alts", false, "Connection uses ALTS if true (this option can only be used on GCP)")
38	altsHSAddr = flag.String("alts_handshaker_service_address", "", "ALTS handshaker gRPC service address")
39	certFile   = flag.String("tls_cert_file", "", "The TLS cert file")
40	keyFile    = flag.String("tls_key_file", "", "The TLS key file")
41	port       = flag.Int("port", 10000, "The server port")
42)
43
44func main() {
45	flag.Parse()
46	if *useTLS && *useALTS {
47		grpclog.Fatalf("use_tls and use_alts cannot be both set to true")
48	}
49	p := strconv.Itoa(*port)
50	lis, err := net.Listen("tcp", ":"+p)
51	if err != nil {
52		grpclog.Fatalf("failed to listen: %v", err)
53	}
54	var opts []grpc.ServerOption
55	if *useTLS {
56		if *certFile == "" {
57			*certFile = testdata.Path("server1.pem")
58		}
59		if *keyFile == "" {
60			*keyFile = testdata.Path("server1.key")
61		}
62		creds, err := credentials.NewServerTLSFromFile(*certFile, *keyFile)
63		if err != nil {
64			grpclog.Fatalf("Failed to generate credentials %v", err)
65		}
66		opts = append(opts, grpc.Creds(creds))
67	} else if *useALTS {
68		altsOpts := alts.DefaultServerOptions()
69		if *altsHSAddr != "" {
70			altsOpts.HandshakerServiceAddress = *altsHSAddr
71		}
72		altsTC := alts.NewServerCreds(altsOpts)
73		opts = append(opts, grpc.Creds(altsTC))
74	}
75	server := grpc.NewServer(opts...)
76	testpb.RegisterTestServiceServer(server, interop.NewTestServer())
77	server.Serve(lis)
78}
79