blob: f8fd12213e66873a717355c2d4f7503a37c6cc20 [file] [log] [blame]
Jiri Simsad7616c92015-03-24 23:44:30 -07001// Copyright 2015 The Vanadium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
Todd Wang555097f2015-04-21 10:49:06 -07005// +build wspr
6//
7// We restrict to a special build-tag in order to enable
8// security.OverrideCaveatValidation, which isn't generally available.
9
Cosmos Nicolaouc80820b2015-03-13 10:34:04 -070010package main
Adam Sadovskyd21d95a2014-10-29 09:56:59 -070011
12import (
13 "flag"
14 "fmt"
15 "io"
Adam Sadovskyd21d95a2014-10-29 09:56:59 -070016
Cosmos Nicolaouc80820b2015-03-13 10:34:04 -070017 "v.io/v23"
18
Todd Wang5b77a342015-04-06 18:31:37 -070019 "v.io/x/ref/services/wspr/wsprlib"
Cosmos Nicolaou1381f8a2015-03-13 09:40:34 -070020 "v.io/x/ref/test/modules"
Adam Sadovskyd21d95a2014-10-29 09:56:59 -070021)
22
23var (
Suharsh Sivakumar9d17e4a2015-02-02 22:42:16 -080024 port *int = flag.CommandLine.Int("port", 0, "Port to listen on.")
25 identd *string = flag.CommandLine.String("identd", "", "identd server name. Must be set.")
Adam Sadovskyd21d95a2014-10-29 09:56:59 -070026)
27
Todd Wang5b77a342015-04-06 18:31:37 -070028const WSPRDCommand = "wsprd"
Cosmos Nicolaouc80820b2015-03-13 10:34:04 -070029
Adam Sadovskyd21d95a2014-10-29 09:56:59 -070030func init() {
Todd Wang555097f2015-04-21 10:49:06 -070031 wsprlib.OverrideCaveatValidation()
Todd Wang5b77a342015-04-06 18:31:37 -070032 modules.RegisterChild(WSPRDCommand, modules.Usage(flag.CommandLine), startWSPR)
Adam Sadovskyd21d95a2014-10-29 09:56:59 -070033}
34
35func startWSPR(stdin io.Reader, stdout, stderr io.Writer, env map[string]string, args ...string) error {
Jiri Simsa6ac95222015-02-23 16:11:49 -080036 ctx, shutdown := v23.Init()
Suharsh Sivakumar6bbd99c2015-01-16 10:57:02 -080037 defer shutdown()
38
Jiri Simsa6ac95222015-02-23 16:11:49 -080039 l := v23.GetListenSpec(ctx)
Todd Wang5b77a342015-04-06 18:31:37 -070040 proxy := wsprlib.NewWSPR(ctx, *port, &l, *identd, nil)
Adam Sadovskyd21d95a2014-10-29 09:56:59 -070041 defer proxy.Shutdown()
42
43 addr := proxy.Listen()
44 go func() {
45 proxy.Serve()
46 }()
47
Cosmos Nicolaou8bd8e102015-01-13 21:52:53 -080048 fmt.Fprintf(stdout, "WSPR_ADDR=%s\n", addr)
Adam Sadovskyd21d95a2014-10-29 09:56:59 -070049 modules.WaitForEOF(stdin)
50 return nil
51}