templates.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. package ovpm
  2. const ccdFileTemplate = `
  3. ifconfig-push {{ .IP }} {{ .NetMask }}
  4. {{if .RedirectGW }}
  5. push "redirect-gateway def1 bypass-dhcp"
  6. {{ end }}
  7. {{range .Servernets}}
  8. push "route {{index . 0}} {{index . 1}}"
  9. {{ end }}
  10. {{range .Routes}}
  11. push "route {{index . 0}} {{index . 1}} {{index . 2}}"
  12. {{ end }}
  13. `
  14. const clientOvpnTemplate = `
  15. # this ovpn file is automatically generated by [OVPM](https://github.com/cad/ovpm)
  16. client
  17. dev tun
  18. server-poll-timeout 4
  19. proto {{ .Proto }}
  20. remote {{ .Hostname }} {{ .Port }}
  21. resolv-retry infinite
  22. ns-cert-type server
  23. cipher AES-128-CBC
  24. nobind
  25. keepalive {{ .KeepalivePeriod }} {{ .KeepaliveTimeout }}
  26. persist-key
  27. persist-tun
  28. {{ if .UseLZO }}comp-lzo{{ end }}
  29. verb 3
  30. auth-nocache
  31. <ca>
  32. {{ .CA }}</ca>
  33. <cert>
  34. {{ .Cert }}</cert>
  35. <key>
  36. {{ .Key }}</key>
  37. `
  38. const dh4096PemTemplate = `
  39. -----BEGIN DH PARAMETERS-----
  40. MIIEDQKCAgEAkeaoNKpzWIU4+8hJUfFeHJJki7lzxK9lVJzoT1O3vRD1pv1M0jm4
  41. d372rq7crH9lolgi0Vyd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MOoG8duJQhluQxka0OhFg0UTGhec11Xs0iTti8CvnOpKqyg2vXlOn0I94P
  58. IVcAwG0ppLn8Py3sBrYO5HF5pStopEPTceylCSkkOT31lVKEAu4TMBrEZh9x2nIV
  59. AHGxQVTkeqx8u7k9Hg02tX9Hm6w8aFF7WCPLJJLe+MWHlOtp4C6ep/PFnc6DESo9
  60. gWCAPLIM7tcb9iSSWBmugSkqVolAgLbX/T95Sxnvn4cwchgtgsoOV2lNUt89B8kf
  61. YtMGCwfzKB0Ws3SW31ayu9jzDgyIr+isjqk1a7MCAgEA
  62. -----END DH PARAMETERS-----
  63. `
  64. const serverConfTemplate = `
  65. ;port 1194
  66. port {{ .Port }}
  67. # TCP or UDP server?
  68. ;proto tcp
  69. proto {{ .Proto }}
  70. # "dev tun" will create a routed IP tunnel,
  71. # "dev tap" will create an ethernet tunnel.
  72. # Use "dev tap0" if you are ethernet bridging
  73. # and have precreated a tap0 virtual interface
  74. # and bridged it with your ethernet interface.
  75. # If you want to control access policies
  76. # over the VPN, you must create firewall
  77. # rules for the the TUN/TAP interface.
  78. # On non-Windows systems, you can give
  79. # an explicit unit number, such as tun0.
  80. # On Windows, use "dev-node" for this.
  81. # On most systems, the VPN will not function
  82. # unless you partially or fully disable
  83. # the firewall for the TUN/TAP interface.
  84. ;dev tap
  85. dev tun
  86. # Windows needs the TAP-Win32 adapter name
  87. # from the Network Connections panel if you
  88. # have more than one. On XP SP2 or higher,
  89. # you may need to selectively disable the
  90. # Windows firewall for the TAP adapter.
  91. # Non-Windows systems usually don't need this.
  92. ;dev-node MyTap
  93. # SSL/TLS root certificate (ca), certificate
  94. # (cert), and private key (key). Each client
  95. # and the server must have their own cert and
  96. # key file. The server and all clients will
  97. # use the same ca file.
  98. #
  99. # See the "easy-rsa" directory for a series
  100. # of scripts for generating RSA certificates
  101. # and private keys. Remember to use
  102. # a unique Common Name for the server
  103. # and each of the client certificates.
  104. #
  105. # Any X509 key management system can be used.
  106. # OpenVPN can also use a PKCS #12 formatted key file
  107. # (see "pkcs12" directive in man page).
  108. ;ca easy-rsa/keys/ca.crt
  109. ;cert easy-rsa/keys/server.crt
  110. ;key easy-rsa/keys/server.key # This file should be kept secret
  111. ca {{ .CACertPath }}
  112. cert {{ .CertPath }}
  113. key {{ .KeyPath }}
  114. # Diffie hellman parameters.
  115. # Generate your own with:
  116. # openssl dhparam -out dh1024.pem 1024
  117. # Substitute 2048 for 1024 if you are using
  118. # 2048 bit keys.
  119. #dh dh1024.pem
  120. ;dh easy-rsa/keys/dh2048.pem
  121. dh {{ .DHParamsPath }}
  122. # Network topology
  123. # Should be subnet (addressing via IP)
  124. # unless Windows clients v2.0.9 and lower have to
  125. # be supported (then net30, i.e. a /30 per client)
  126. # Defaults to net30 (not recommended)
  127. topology subnet
  128. # Configure server mode and supply a VPN subnet
  129. # for OpenVPN to draw client addresses from.
  130. # The server will take 10.8.0.1 for itself,
  131. # the rest will be made available to clients.
  132. # Each client will be able to reach the server
  133. # on 10.8.0.1. Comment this line out if you are
  134. # ethernet bridging. See the man page for more info.
  135. ;server 10.8.0.0 255.255.255.0
  136. server {{ .Net }} {{ .Mask }}
  137. # Maintain a record of client <-> virtual IP address
  138. # associations in this file. If OpenVPN goes down or
  139. # is restarted, reconnecting clients can be assigned
  140. # the same virtual IP address from the pool that was
  141. # previously assigned.
  142. ifconfig-pool-persist ipp.txt
  143. # Configure server mode for ethernet bridging.
  144. # You must first use your OS's bridging capability
  145. # to bridge the TAP interface with the ethernet
  146. # NIC interface. Then you must manually set the
  147. # IP/netmask on the bridge interface, here we
  148. # assume 10.8.0.4/255.255.255.0. Finally we
  149. # must set aside an IP range in this subnet
  150. # (start=10.8.0.50 end=10.8.0.100) to allocate
  151. # to connecting clients. Leave this line commented
  152. # out unless you are ethernet bridging.
  153. ;server-bridge 10.8.0.4 255.255.255.0 10.8.0.50 10.8.0.100
  154. # Configure server mode for ethernet bridging
  155. # using a DHCP-proxy, where clients talk
  156. # to the OpenVPN server-side DHCP server
  157. # to receive their IP address allocation
  158. # and DNS server addresses. You must first use
  159. # your OS's bridging capability to bridge the TAP
  160. # interface with the ethernet NIC interface.
  161. # Note: this mode only works on clients (such as
  162. # Windows), where the client-side TAP adapter is
  163. # bound to a DHCP client.
  164. ;server-bridge
  165. # Push routes to the client to allow it
  166. # to reach other private subnets behind
  167. # the server. Remember that these
  168. # private subnets will also need
  169. # to know to route the OpenVPN client
  170. # address pool (10.8.0.0/255.255.255.0)
  171. # back to the OpenVPN server.
  172. #route 192.168.90.0 255.255.255.0 10.8.0.2
  173. #route 192.168.91.0 255.255.255.0 10.8.0.5
  174. #route 192.168.92.0 255.255.255.0 10.8.0.3
  175. #route 192.168.93.0 255.255.255.0 10.8.0.4
  176. #push "route 172.16.100.0 255.255.255.0"
  177. #push "route 192.168.60.0 255.255.255.0"
  178. # To assign specific IP addresses to specific
  179. # clients or if a connecting client has a private
  180. # subnet behind it that should also have VPN access,
  181. # use the subdirectory "ccd" for client-specific
  182. # configuration files (see man page for more info).
  183. # EXAMPLE: Suppose the client
  184. # having the certificate common name "Thelonious"
  185. # also has a small subnet behind his connecting
  186. # machine, such as 192.168.40.128/255.255.255.248.
  187. # First, uncomment out these lines:
  188. #client-config-dir ccd
  189. #route 192.168.90.0 255.255.255.0
  190. # Then create a file ccd/Thelonious with this line:
  191. # iroute 192.168.40.128 255.255.255.248
  192. # This will allow Thelonious' private subnet to
  193. # access the VPN. This example will only work
  194. # if you are routing, not bridging, i.e. you are
  195. # using "dev tun" and "server" directives.
  196. # EXAMPLE: Suppose you want to give
  197. # Thelonious a fixed VPN IP address of 10.9.0.1.
  198. # First uncomment out these lines:
  199. ;client-config-dir ccd
  200. client-config-dir {{ .CCDPath }}
  201. # Then add this line to ccd/Thelonious:
  202. # ifconfig-push 10.9.0.1 10.9.0.2
  203. crl-verify {{ .CRLPath }}
  204. # Suppose that you want to enable different
  205. # firewall access policies for different groups
  206. # of clients. There are two methods:
  207. # (1) Run multiple OpenVPN daemons, one for each
  208. # group, and firewall the TUN/TAP interface
  209. # for each group/daemon appropriately.
  210. # (2) (Advanced) Create a script to dynamically
  211. # modify the firewall in response to access
  212. # from different clients. See man
  213. # page for more info on learn-address script.
  214. ;learn-address ./script
  215. # If enabled, this directive will configure
  216. # all clients to redirect their default
  217. # network gateway through the VPN, causing
  218. # all IP traffic such as web browsing and
  219. # and DNS lookups to go through the VPN
  220. # (The OpenVPN server machine may need to NAT
  221. # or bridge the TUN/TAP interface to the internet
  222. # in order for this to work properly).
  223. ;push "redirect-gateway def1 bypass-dhcp"
  224. ;push "redirect-gateway def1 bypass-dhcp"
  225. # Certain Windows-specific network settings
  226. # can be pushed to clients, such as DNS
  227. # or WINS server addresses. CAVEAT:
  228. # http://openvpn.net/faq.html#dhcpcaveats
  229. # The addresses below refer to the public
  230. # DNS servers provided by opendns.com.
  231. ;push "dhcp-option DNS 208.67.222.222"
  232. push "dhcp-option DNS {{ .DNS }}"
  233. # Uncomment this directive to allow different
  234. # clients to be able to "see" each other.
  235. # By default, clients will only see the server.
  236. # To force clients to only see the server, you
  237. # will also need to appropriately firewall the
  238. # server's TUN/TAP interface.
  239. client-to-client
  240. # Uncomment this directive if multiple clients
  241. # might connect with the same certificate/key
  242. # files or common names. This is recommended
  243. # only for testing purposes. For production use,
  244. # each client should have its own certificate/key
  245. # pair.
  246. #
  247. # IF YOU HAVE NOT GENERATED INDIVIDUAL
  248. # CERTIFICATE/KEY PAIRS FOR EACH CLIENT,
  249. # EACH HAVING ITS OWN UNIQUE "COMMON NAME",
  250. # UNCOMMENT THIS LINE OUT.
  251. ;duplicate-cn
  252. # The keepalive directive causes ping-like
  253. # messages to be sent back and forth over
  254. # the link so that each side knows when
  255. # the other side has gone down.
  256. # Ping every 10 seconds, assume that remote
  257. # peer is down if no ping received during
  258. # a 120 second time period.
  259. ;keepalive 10 120
  260. keepalive {{ .KeepalivePeriod }} {{ .KeepaliveTimeout }}
  261. # For extra security beyond that provided
  262. # by SSL/TLS, create an "HMAC firewall"
  263. # to help block DoS attacks and UDP port flooding.
  264. #
  265. # Generate with:
  266. # openvpn --genkey --secret ta.key
  267. #
  268. # The server and each client must have
  269. # a copy of this key.
  270. # The second parameter should be '0'
  271. # on the server and '1' on the clients.
  272. ;tls-auth ta.key 0 # This file is secret
  273. # Select a cryptographic cipher.
  274. # This config item must be copied to
  275. # the client config file as well.
  276. ;cipher BF-CBC # Blowfish (default)
  277. ;cipher AES-128-CBC # AES
  278. ;cipher DES-EDE3-CBC # Triple-DES
  279. cipher AES-128-CBC
  280. {{ if .UseLZO }}
  281. # Enable compression on the VPN link.
  282. # If you enable it here, you must also
  283. # enable it in the client config file.
  284. comp-lzo{{ end }}
  285. # The maximum number of concurrently connected
  286. # clients we want to allow.
  287. ;max-clients 100
  288. # It's a good idea to reduce the OpenVPN
  289. # daemon's privileges after initialization.
  290. #
  291. # You can uncomment this out on
  292. # non-Windows systems.
  293. user nobody
  294. group nogroup
  295. # The persist options will try to avoid
  296. # accessing certain resources on restart
  297. # that may no longer be accessible because
  298. # of the privilege downgrade.
  299. persist-key
  300. persist-tun
  301. # Output a short status file showing
  302. # current connections, truncated
  303. # and rewritten every minute.
  304. status openvpn-status.log 5
  305. # By default, log messages will go to the syslog (or
  306. # on Windows, if running as a service, they will go to
  307. # the "\Program Files\OpenVPN\log" directory).
  308. # Use log or log-append to override this default.
  309. # "log" will truncate the log file on OpenVPN startup,
  310. # while "log-append" will append to it. Use one
  311. # or the other (but not both).
  312. ;log openvpn.log
  313. log-append openvpn.log
  314. # Set the appropriate level of log
  315. # file verbosity.
  316. #
  317. # 0 is silent, except for fatal errors
  318. # 4 is reasonable for general usage
  319. # 5 and 6 can help to debug connection problems
  320. # 9 is extremely verbose
  321. verb 3
  322. # Silence repeating messages. At most 20
  323. # sequential messages of the same message
  324. # category will be output to the log.
  325. ;mute 20
  326. `