main.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*
  2. * Copyright (c) 2016, Psiphon Inc.
  3. * All rights reserved.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. package main
  20. import (
  21. "crypto/x509"
  22. "encoding/base64"
  23. "encoding/json"
  24. "encoding/pem"
  25. "flag"
  26. "fmt"
  27. "io/ioutil"
  28. "os"
  29. "path/filepath"
  30. "strconv"
  31. "time"
  32. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/osl"
  33. )
  34. func main() {
  35. var configFilename string
  36. flag.StringVar(&configFilename, "config", "", "OSL configuration filename")
  37. var offset time.Duration
  38. flag.DurationVar(
  39. &offset, "offset", 0,
  40. "pave OSL start time (offset from now); default, 0, selects earliest epoch")
  41. var period time.Duration
  42. flag.DurationVar(
  43. &period, "period", 0,
  44. "pave OSL total period (starting from offset); default, 0, selects at least one OSL period from now for all schemes")
  45. var signingKeyPairFilename string
  46. flag.StringVar(&signingKeyPairFilename, "key", "", "signing public key pair filename")
  47. var payloadFilename string
  48. flag.StringVar(&payloadFilename, "payload", "", "server entries to pave into OSLs")
  49. var destinationDirectory string
  50. flag.StringVar(
  51. &destinationDirectory, "output", "",
  52. "destination directory for output files; when omitted, no files are written (dry run mode)")
  53. var listScheme int
  54. flag.IntVar(&listScheme, "list-scheme", -1, "list current period OSL IDs for specified scheme; no files are written")
  55. var omitMD5SumsSchemes ints
  56. flag.Var(&omitMD5SumsSchemes, "omit-md5sums", "omit MD5Sum fields for specified scheme(s)")
  57. var omitEmptyOSLsSchemes ints
  58. flag.Var(&omitEmptyOSLsSchemes, "omit-empty", "omit empty OSLs for specified scheme(s)")
  59. flag.Parse()
  60. // load config
  61. configJSON, err := ioutil.ReadFile(configFilename)
  62. if err != nil {
  63. fmt.Printf("failed loading configuration file: %s\n", err)
  64. os.Exit(1)
  65. }
  66. config, err := osl.LoadConfig(configJSON)
  67. if err != nil {
  68. fmt.Printf("failed processing configuration file: %s\n", err)
  69. os.Exit(1)
  70. }
  71. if listScheme != -1 {
  72. OSLIDs, err := config.CurrentOSLIDs(listScheme)
  73. if err != nil {
  74. fmt.Printf("failed listing scheme OSL IDs: %s\n", err)
  75. os.Exit(1)
  76. }
  77. for propagationChannelID, OSLID := range OSLIDs {
  78. fmt.Printf("%s %s\n", propagationChannelID, OSLID)
  79. }
  80. return
  81. }
  82. // load key pair
  83. keyPairPEM, err := ioutil.ReadFile(signingKeyPairFilename)
  84. if err != nil {
  85. fmt.Printf("failed loading signing public key pair file: %s\n", err)
  86. os.Exit(1)
  87. }
  88. // Password "none" from psi_ops:
  89. // https://bitbucket.org/psiphon/psiphon-circumvention-system/src/ef4f3d4893bd5259ef24f0cb4525cbbbb0854cf9/Automation/psi_ops.py?at=default&fileviewer=file-view-default#psi_ops.py-297
  90. block, _ := pem.Decode(keyPairPEM)
  91. decryptedKeyPairPEM, err := x509.DecryptPEMBlock(block, []byte("none"))
  92. if err != nil {
  93. fmt.Printf("failed decrypting signing public key pair file: %s\n", err)
  94. os.Exit(1)
  95. }
  96. rsaKey, err := x509.ParsePKCS1PrivateKey(decryptedKeyPairPEM)
  97. if err != nil {
  98. fmt.Printf("failed parsing signing public key pair file: %s\n", err)
  99. os.Exit(1)
  100. }
  101. publicKeyBytes, err := x509.MarshalPKIXPublicKey(rsaKey.Public())
  102. if err != nil {
  103. fmt.Printf("failed marshaling signing public key: %s\n", err)
  104. os.Exit(1)
  105. }
  106. privateKeyBytes := x509.MarshalPKCS1PrivateKey(rsaKey)
  107. signingPublicKey := base64.StdEncoding.EncodeToString(publicKeyBytes)
  108. signingPrivateKey := base64.StdEncoding.EncodeToString(privateKeyBytes)
  109. // load payload
  110. paveServerEntries := make(map[string][]string)
  111. pavedPayloadOSLID := make(map[string]bool)
  112. if payloadFilename != "" {
  113. payloadJSON, err := ioutil.ReadFile(payloadFilename)
  114. if err != nil {
  115. fmt.Printf("failed loading payload file: %s\n", err)
  116. os.Exit(1)
  117. }
  118. var payload []*struct {
  119. OSLIDs []string
  120. ServerEntry string
  121. }
  122. err = json.Unmarshal(payloadJSON, &payload)
  123. if err != nil {
  124. fmt.Printf("failed unmarshaling payload file: %s\n", err)
  125. os.Exit(1)
  126. }
  127. for _, item := range payload {
  128. for _, oslID := range item.OSLIDs {
  129. paveServerEntries[oslID] = append(
  130. paveServerEntries[oslID], item.ServerEntry)
  131. pavedPayloadOSLID[oslID] = false
  132. }
  133. }
  134. }
  135. // determine pave time range
  136. paveTime := time.Now().UTC()
  137. var startTime, endTime time.Time
  138. if offset != 0 {
  139. startTime = paveTime.Add(offset)
  140. } else {
  141. // Default to the earliest scheme epoch.
  142. startTime = paveTime
  143. for _, scheme := range config.Schemes {
  144. epoch, _ := time.Parse(time.RFC3339, scheme.Epoch)
  145. if epoch.Before(startTime) {
  146. startTime = epoch
  147. }
  148. }
  149. }
  150. if period != 0 {
  151. endTime = startTime.Add(period)
  152. } else {
  153. // Default to at least one OSL period after "now",
  154. // considering all schemes.
  155. endTime = paveTime
  156. for _, scheme := range config.Schemes {
  157. oslDuration := scheme.GetOSLDuration()
  158. if endTime.Add(oslDuration).After(endTime) {
  159. endTime = endTime.Add(oslDuration)
  160. }
  161. }
  162. }
  163. // build list of all participating propagation channel IDs
  164. allPropagationChannelIDs := make(map[string]bool)
  165. for _, scheme := range config.Schemes {
  166. for _, propagationChannelID := range scheme.PropagationChannelIDs {
  167. allPropagationChannelIDs[propagationChannelID] = true
  168. }
  169. }
  170. // pave a directory for each propagation channel
  171. for propagationChannelID := range allPropagationChannelIDs {
  172. paveFiles, err := config.Pave(
  173. endTime,
  174. propagationChannelID,
  175. signingPublicKey,
  176. signingPrivateKey,
  177. paveServerEntries,
  178. omitMD5SumsSchemes,
  179. omitEmptyOSLsSchemes,
  180. func(logInfo *osl.PaveLogInfo) {
  181. pavedPayloadOSLID[logInfo.OSLID] = true
  182. fmt.Printf(
  183. "paved %s: scheme %d, propagation channel ID %s, "+
  184. "OSL time %s, OSL duration %s, server entries: %d\n",
  185. logInfo.FileName,
  186. logInfo.SchemeIndex,
  187. logInfo.PropagationChannelID,
  188. logInfo.OSLTime,
  189. logInfo.OSLDuration,
  190. logInfo.ServerEntryCount)
  191. })
  192. if err != nil {
  193. fmt.Printf("failed paving: %s\n", err)
  194. os.Exit(1)
  195. }
  196. if destinationDirectory != "" {
  197. directory := filepath.Join(destinationDirectory, propagationChannelID)
  198. err = os.MkdirAll(directory, 0755)
  199. if err != nil {
  200. fmt.Printf("failed creating output directory: %s\n", err)
  201. os.Exit(1)
  202. }
  203. for _, paveFile := range paveFiles {
  204. filename := filepath.Join(directory, paveFile.Name)
  205. err = ioutil.WriteFile(filename, paveFile.Contents, 0755)
  206. if err != nil {
  207. fmt.Printf("error writing output file: %s\n", err)
  208. os.Exit(1)
  209. }
  210. }
  211. }
  212. }
  213. // fail if payload contains OSL IDs not in the config and time range
  214. unknown := false
  215. for oslID, paved := range pavedPayloadOSLID {
  216. if !paved {
  217. fmt.Printf(
  218. "ignored %d server entries for unknown OSL ID: %s\n",
  219. len(paveServerEntries[oslID]),
  220. oslID)
  221. unknown = true
  222. }
  223. }
  224. if unknown {
  225. fmt.Printf("payload contains unknown OSL IDs\n")
  226. os.Exit(1)
  227. }
  228. }
  229. type ints []int
  230. func (i *ints) String() string {
  231. return fmt.Sprint(*i)
  232. }
  233. func (i *ints) Set(strValue string) error {
  234. value, err := strconv.Atoi(strValue)
  235. if err != nil {
  236. return err
  237. }
  238. *i = append(*i, value)
  239. return nil
  240. }