1/*
2 * Copyright (c) 2014, Yawning Angel <yawning at schwanenlied dot me>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 *  * Redistributions of source code must retain the above copyright notice,
9 *    this list of conditions and the following disclaimer.
10 *
11 *  * Redistributions in binary form must reproduce the above copyright notice,
12 *    this list of conditions and the following disclaimer in the documentation
13 *    and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
26 */
27
28// Package transports provides a interface to query supported pluggable
29// transports.
30package transports // import "gitlab.com/yawning/obfs4.git/transports"
31
32import (
33	"fmt"
34	"sync"
35
36	"gitlab.com/yawning/obfs4.git/transports/base"
37	"gitlab.com/yawning/obfs4.git/transports/meeklite"
38	"gitlab.com/yawning/obfs4.git/transports/obfs2"
39	"gitlab.com/yawning/obfs4.git/transports/obfs3"
40	"gitlab.com/yawning/obfs4.git/transports/obfs4"
41	"gitlab.com/yawning/obfs4.git/transports/scramblesuit"
42)
43
44var transportMapLock sync.Mutex
45var transportMap map[string]base.Transport = make(map[string]base.Transport)
46
47// Register registers a transport protocol.
48func Register(transport base.Transport) error {
49	transportMapLock.Lock()
50	defer transportMapLock.Unlock()
51
52	name := transport.Name()
53	_, registered := transportMap[name]
54	if registered {
55		return fmt.Errorf("transport '%s' already registered", name)
56	}
57	transportMap[name] = transport
58
59	return nil
60}
61
62// Transports returns the list of registered transport protocols.
63func Transports() []string {
64	transportMapLock.Lock()
65	defer transportMapLock.Unlock()
66
67	var ret []string
68	for name := range transportMap {
69		ret = append(ret, name)
70	}
71
72	return ret
73}
74
75// Get returns a transport protocol implementation by name.
76func Get(name string) base.Transport {
77	transportMapLock.Lock()
78	defer transportMapLock.Unlock()
79
80	t := transportMap[name]
81
82	return t
83}
84
85// Init initializes all of the integrated transports.
86func Init() error {
87	for _, v := range []base.Transport{
88		new(meeklite.Transport),
89		new(obfs2.Transport),
90		new(obfs3.Transport),
91		new(obfs4.Transport),
92		new(scramblesuit.Transport),
93	} {
94		if err := Register(v); err != nil {
95			return err
96		}
97	}
98
99	return nil
100}
101