-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
95 lines (75 loc) · 2.48 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
variable "do_api_token" {}
variable "vpn_instance_name" {
description = "Instance name to use"
default = ""
}
variable "region" {
description = "An identifier of DigitalOcean region to put VPN/proxy instance to"
default = "nyc2"
}
# VPN credentials
resource "random_string" "vpn_user" {
length = 6
special = false
}
resource "random_string" "vpn_password" {
length = 8
special = false
}
resource "random_string" "vpn_psk" {
length = 8
special = false
}
# SOCKS5 credentials
resource "random_string" "socks_user" {
length = 6
special = false
}
resource "random_string" "socks_password" {
length = 8
special = false
}
# Set up DigitalOcean instance running VPN and SOCKS5 proxy servers
provider "digitalocean" {
token = "${var.do_api_token}"
}
resource "random_id" "vpn_instance_name" {
prefix = "vpn-"
byte_length = 4
}
resource "tls_private_key" "vpn" {
algorithm = "RSA"
rsa_bits = "4096"
}
resource "digitalocean_ssh_key" "vpn" {
name = "VPN server"
public_key = "${tls_private_key.vpn.public_key_openssh}"
}
resource "digitalocean_droplet" "vpn" {
image = "ubuntu-18-04-x64"
name = "${var.vpn_instance_name != "" ? var.vpn_instance_name : random_id.vpn_instance_name.hex}"
size = "s-1vcpu-1gb"
region = "${var.region}"
ssh_keys = ["${digitalocean_ssh_key.vpn.fingerprint}"]
connection {
type = "ssh"
user = "root"
private_key = "${tls_private_key.vpn.private_key_pem}"
timeout = "2m"
}
provisioner "remote-exec" {
inline = <<-EOF
# install Docker CE according to https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-on-ubuntu-18-04
apt update
apt -y install apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -
add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic stable"
apt update
apt install -y docker-ce
# install l2tp VPN server
docker run -d --cap-add=NET_ADMIN -p 500:500/udp -p 4500:4500/udp -p 1701:1701/udp -p 1701:1701/tcp --restart=unless-stopped -e PSK=${random_string.vpn_psk.result} -e USERS=${random_string.vpn_user.result}:${random_string.vpn_password.result} siomiz/softethervpn:alpine
# install socks5 proxy
docker run -d --restart=unless-stopped -p 1080:1080 -e PORT=1080 -e USER=${random_string.socks_user.result} -e PASS=${random_string.socks_password.result} schors/tgdante2:latest
EOF
}
}