Advertisement
Guest User

Untitled

a guest
Aug 2nd, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  1. package gumble
  2.  
  3. import (
  4. "time"
  5.  
  6. "layeh.com/gumble/gumble"
  7. _ "layeh.com/gumble/opus"
  8. "crypto/tls"
  9. "fmt"
  10. "os"
  11. "log"
  12. "path/filepath"
  13. "crypto/rsa"
  14. "crypto/rand"
  15. "crypto/x509"
  16. "net"
  17. "github.com/Stonetium/stonetium/errs"
  18. )
  19.  
  20. func GumbleInit(server, user, pass string, nocert bool) *StonetiumVoice {
  21.  
  22. // Initialize
  23. s := NewStonetiumVoice(nil)
  24. s.Config = gumble.NewConfig()
  25. s.Server = server
  26.  
  27. //Set AudioInterval to 40ms from default 10ms for improving audio quality
  28. s.Config.AudioInterval = time.Millisecond * 60
  29. s.Config.AudioDataBytes = 80
  30.  
  31. s.Config.Username = user
  32. s.Config.Password = pass
  33.  
  34. s.TLSConfig.InsecureSkipVerify = true
  35.  
  36. dirPath, err := filepath.Abs(filepath.Dir(os.Args[0]))
  37. if err != nil {
  38. log.Println("Cannot find dir path:", dirPath)
  39. }
  40.  
  41. if _, err := os.Stat(dirPath + "/certificate.pem"); os.IsNotExist(err) {
  42. log.Println("Mumble certificate file not found! generating one for you")
  43.  
  44. // generate a new key-pair
  45. rootKey, err := rsa.GenerateKey(rand.Reader, 2048)
  46. if err != nil {
  47. log.Fatalf("generating random key: %v", err)
  48. }
  49.  
  50. f, err := os.Create(dirPath + "/private.key")
  51. defer f.Close()
  52.  
  53. privkeyPEM := ExportRsaPrivateKeyAsPemStr(rootKey)
  54. _, err = f.Write([]byte(privkeyPEM))
  55. if err != nil {
  56. log.Println("cannot write privKey bytes to file:", errs.Stack(err))
  57. }
  58.  
  59. rootCertTmpl, err := CertTemplate()
  60. if err != nil {
  61. log.Fatalf("creating cert template: %v", err)
  62. }
  63. // describe what the certificate will be used for
  64. rootCertTmpl.IsCA = true
  65. rootCertTmpl.KeyUsage = x509.KeyUsageCertSign | x509.KeyUsageDigitalSignature
  66. rootCertTmpl.ExtKeyUsage = []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth, x509.ExtKeyUsageClientAuth}
  67. rootCertTmpl.IPAddresses = []net.IP{net.ParseIP("127.0.0.1")}
  68.  
  69. rootCert, rootCertPEM, err := CreateCert(rootCertTmpl, rootCertTmpl, &rootKey.PublicKey, rootKey)
  70. if err != nil {
  71. log.Fatalf("error creating cert: %v", err)
  72. }
  73. fmt.Printf("%s\n", rootCertPEM)
  74. fmt.Printf("%#x\n", rootCert.Signature) // more ugly binary
  75.  
  76. f, err = os.Create(dirPath + "/certificate.pem")
  77. defer f.Close()
  78.  
  79. _, err = f.Write(rootCertPEM)
  80. if err != nil {
  81. log.Println(errs.Stack(err))
  82. }
  83. } else {
  84. log.Println("Found certificate.pem! Loading into Mumble client...")
  85. }
  86.  
  87. cert, err := tls.LoadX509KeyPair(dirPath + "/certificate.pem", dirPath + "/private.key")
  88. if err != nil {
  89. fmt.Fprintf(os.Stderr, "%s\n", err)
  90. os.Exit(1)
  91. }
  92.  
  93. s.TLSConfig.Certificates = append(s.TLSConfig.Certificates, cert)
  94.  
  95. go s.start()
  96.  
  97. return s
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement