func Example() { saver := new(savers.FileSystemSaver) saver.Report = true saver.Duration = time.Second * 3 // Run before any v1 or v2 UUIDs to ensure the saver takes uuid.RegisterSaver(saver) u1 := uuid.NewV1() fmt.Printf("version %d variant %x: %s\n", u1.Version(), u1.Variant(), u1) uP, _ := uuid.Parse("6ba7b810-9dad-11d1-80b4-00c04fd430c8") u3 := uuid.NewV3(uP, uuid.Name("test")) u4 := uuid.NewV4() fmt.Printf("version %d variant %x: %s\n", u4.Version(), u4.Variant(), u4) u5 := uuid.NewV5(uuid.NamespaceURL, uuid.Name("test")) if uuid.Equal(u1, u3) { fmt.Printf("Will never happen") } fmt.Print(uuid.Sprintf(uuid.CurlyHyphen, u5)) uuid.SwitchFormat(uuid.BracketHyphen) }
func ParseMessage(recipients []string, sender string, data []byte) (msg Msg, err error) { msg.Sender, err = ParseAddress(sender) if err != nil { return msg, err } for _, rcpt := range recipients { rcptAddr, err := ParseAddress(rcpt) if err != nil { return msg, err } msg.Rcpt = append(msg.Rcpt, rcptAddr) } message, err := mail.ReadMessage(bytes.NewReader(data)) if err != nil { return msg, err } msg.Message = *message msg.MessageId = msg.Message.Header.Get("message-id") if msg.MessageId == "" { id := uuid.NewV4() uuid.SwitchFormat(uuid.Clean) msg.MessageId = id.String() } msg.RcptDomains = make(map[string]int) for _, d := range msg.Rcpt { msg.RcptDomains[d.Domain] = msg.RcptDomains[d.Domain] + 1 } return msg, nil }
// getMessagesOutput wraps an MDS message into a GetMessagesOutput struct. func getMessagesOutput(m *ssmmds.Message) ssmmds.GetMessagesOutput { uuid.SwitchFormat(uuid.CleanHyphen) requestID := uuid.NewV4().String() return ssmmds.GetMessagesOutput{ Destination: m.Destination, Messages: []*ssmmds.Message{m}, MessagesRequestId: aws.String(requestID), } }
// SendReply calls the SendReply MDS API. func (mds *sdkService) SendReply(log log.T, messageID string, payload string) (err error) { uuid.SwitchFormat(uuid.CleanHyphen) replyID := uuid.NewV4().String() params := &ssmmds.SendReplyInput{ MessageId: aws.String(messageID), // Required Payload: aws.String(payload), // Required ReplyId: aws.String(replyID), // Required } log.Debug("Calling SendReply with params", params) req, resp := mds.sdk.SendReplyRequest(params) if err = mds.sendRequest(req); err != nil { err = fmt.Errorf("SendReply Error: %v", err) log.Debug(err) } else { log.Info("SendReply Response", resp) } return }
func Example() { var config = uuid.StateSaverConfig{SaveReport: true, SaveSchedule: 30 * time.Minute} uuid.SetupFileSystemStateSaver(config) u1 := uuid.NewV1() fmt.Printf("version %d variant %x: %s\n", u1.Version(), u1.Variant(), u1) uP, _ := uuid.Parse("6ba7b810-9dad-11d1-80b4-00c04fd430c8") u3 := uuid.NewV3(uP, uuid.Name("test")) u4 := uuid.NewV4() fmt.Printf("version %d variant %x: %s\n", u4.Version(), u4.Variant(), u4) u5 := uuid.NewV5(uuid.NamespaceURL, uuid.Name("test")) if uuid.Equal(u1, u3) { fmt.Printf("Will never happen") } fmt.Printf(uuid.Formatter(u5, uuid.CurlyHyphen)) uuid.SwitchFormat(uuid.BracketHyphen) }
func addLabel(name string) (*Label, error) { // Get user token savedToken, err := getSavedToken() check(err) token := savedToken.Token // Generate some UUID to avoid duplicates uuid.SwitchFormat(uuid.CleanHyphen) generatedUUID := uuid.NewV4().String() tmpID := uuid.NewV4().String() // Create Label instance label := Label{} label.Name = url.QueryEscape(name) bytes, _ := json.Marshal(label) // Launch the HTTP request url := fmt.Sprintf("https://todoist.com/API/v6/sync?token=%s&commands=[{\"type\":\"label_add\",\"temp_id\":\"%s\",\"uuid\":\"%s\",\"args\":%s}]", token, tmpID, generatedUUID, string(bytes)) resp, err := http.Get(url) check(err) defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) check(err) // Error case if resp.StatusCode != http.StatusOK { return nil, errors.New("An error occured during the label creation \"" + name + "\": " + resp.Status + "; response: " + string(body) + ")") } // Success var status SyncStatus err = json.Unmarshal(body, &status) check(err) label.Id = status.TempIdMapping[tmpID] return &label, nil }
func Example() { saver := new(savers.FileSystemSaver) saver.Report = true saver.Duration = time.Second * 3 // Run before any v1 or v2 UUIDs to ensure the savers takes uuid.RegisterSaver(saver) up, _ := uuid.Parse("6ba7b810-9dad-11d1-80b4-00c04fd430c8") fmt.Printf("version %d variant %x: %s\n", up.Version(), up.Variant(), up) uuid.New(up.Bytes()) u1 := uuid.NewV1() fmt.Printf("version %d variant %x: %s\n", u1.Version(), u1.Variant(), u1) u4 := uuid.NewV4() fmt.Printf("version %d variant %x: %s\n", u4.Version(), u4.Variant(), u4) u3 := uuid.NewV3(u1, u4) url, _ := url.Parse("www.example.com") u5 := uuid.NewV5(uuid.NameSpaceURL, url) if uuid.Equal(u1, u3) { fmt.Println("Will never happen") } if uuid.Compare(uuid.NameSpaceDNS, uuid.NameSpaceDNS) == 0 { fmt.Println("They are equal") } // Default Format is Canonical fmt.Println(uuid.Formatter(u5, uuid.FormatCanonicalCurly)) uuid.SwitchFormat(uuid.FormatCanonicalBracket) }
// generateFingerprint generates new fingerprint and saves it in the vault func generateFingerprint() (string, error) { uuid.SwitchFormat(uuid.CleanHyphen) result := "" // fetch current hardware hash values hardwareHash := currentHwHash() // try get previously saved fingerprint data from vault savedHwInfo, err := fetch() if err != nil { return "", err } threshold := minimumMatchPercent if savedHwInfo.SimilarityThreshold >= 0 { threshold = savedHwInfo.SimilarityThreshold } // check if this is the first time we are generating the fingerprint // or if there is no match if savedHwInfo.Fingerprint == "" || !isSimilarHardwareHash(savedHwInfo.HardwareHash, hardwareHash, threshold) { // generate new fingerprint result = uuid.NewV4().String() } else { result = savedHwInfo.Fingerprint } // generate updated info to save to vault updatedHwInfo := hwInfo{ Fingerprint: result, HardwareHash: hardwareHash, SimilarityThreshold: threshold, } // save content in vault save(updatedHwInfo) return result, nil }
// GetMessages calls the GetMessages MDS API. func (mds *sdkService) GetMessages(log log.T, instanceID string) (messages *ssmmds.GetMessagesOutput, err error) { uuid.SwitchFormat(uuid.CleanHyphen) uid := uuid.NewV4().String() params := &ssmmds.GetMessagesInput{ Destination: aws.String(instanceID), // Required MessagesRequestId: aws.String(uid), // Required VisibilityTimeoutInSeconds: aws.Int64(10), } log.Debug("Calling GetMessages with params", params) requestTime := time.Now() req, messages := mds.sdk.GetMessagesRequest(params) if requestErr := mds.sendRequest(req); requestErr != nil { log.Debug(requestErr) if isErrorUnexpected(log, requestErr, requestTime, time.Now()) { //GetMessages api responded with unexpected errors - we must return this as error err = fmt.Errorf("GetMessages Error: %v", requestErr) log.Debug(err) } } else { log.Debug("GetMessages Response", messages) } return }
// generateNewUniqueCode generates a new confirmation code func generateNewUniqueCode() string { // set code format uuid.SwitchFormat(uuid.Clean) return uuid.NewV4().String() }
func ExampleSwitchFormat() { uuid.SwitchFormat(uuid.BracketHyphen) u4 := uuid.NewV4() fmt.Printf("version %d variant %x: %s\n", u4.Version(), u4.Variant(), u4) }
func init() { // TODO: wtf, this is lame uuid.SwitchFormat(uuid.CleanHyphen) }
func init() { // Change the UUID format to remove surrounding braces and dashes uuid.SwitchFormat(uuid.Clean, true) }
// AddTask adds a task to a user todoist project func AddTask(taskContent string, projectOrder int, date string, rawLabels string, priority int) { // Get user token savedToken, err := getSavedToken() check(err) token := savedToken.Token // Retrieve the project project, err := getProjectFromOrder(projectOrder) check(err) // Generate some UUID to avoid duplicates uuid.SwitchFormat(uuid.CleanHyphen) generatedUUID := uuid.NewV4().String() tmpID := uuid.NewV4().String() // Get labels id strLabels := strings.Split(rawLabels, ",") labels := make([]int, len(strLabels)) for index, labelName := range strLabels { label, err := getLabelFromName(labelName) if err != nil { createdLabel, _ := addLabel(labelName) if err != nil { labels[index] = createdLabel.Id } continue } labels[index] = label.Id } // Ensure the task is URL-safe taskContent = url.QueryEscape(taskContent) date = url.QueryEscape(date) task := Task{} task.Content = taskContent task.Priority = priority task.DateLang = "en" task.Date = date task.ProjectId = project.Id if len(labels) > 0 { task.Labels = labels } bytes, _ := json.Marshal(task) // Launch the HTTP request url := fmt.Sprintf("https://todoist.com/API/v6/sync?token=%s&commands=[{\"type\":\"item_add\",\"temp_id\":\"%s\",\"uuid\":\"%s\",\"args\":%s}]", token, tmpID, generatedUUID, string(bytes)) resp, err := http.Get(url) check(err) defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) check(err) // Error case if resp.StatusCode != http.StatusOK { ct.ChangeColor(ct.Red, false, ct.None, false) fmt.Println("An error occured during the task adding (HTTP status: " + resp.Status + "; response: " + string(body) + ")") ct.ResetColor() return } // Success ct.ChangeColor(ct.Green, false, ct.None, false) fmt.Println("Task successfully added to the project: " + project.Name) ct.ResetColor() }
func main() { verbose := flag.Bool("v", false, "should every proxy request be logged to stdout") mongourl := flag.String("mongourl", "", "record request/response in mongodb") mock := flag.Bool("m", false, "send fake responses") addr := flag.String("l", ":8080", "on which address should the proxy listen") flag.Parse() tmpdir := filepath.Join(os.TempDir(), "proxy-service") if _, err := os.Stat(tmpdir); err != nil { if os.IsNotExist(err) { // Create temp directory to store body response err = os.MkdirAll(tmpdir, 0777) } // err should be nil if we just created the directory if err != nil { panic(err) } } db := new(mgo.Database) c := new(mgo.Collection) h := new(mgo.Collection) rules := new(mgo.Collection) if len(*mongourl) != 0 { // Mongo DB connection session, err := mgo.Dial(*mongourl) if err != nil { panic(err) } defer session.Close() // Optional. Switch the session to a monotonic behavior. session.SetMode(mgo.Monotonic, true) db = session.DB("proxyservice") c = db.C("log_logentry") h = db.C("log_hostrewrite") rules = db.C("log_rules") } else { db = nil c = nil h = nil rules = nil } uuid.SwitchFormat(uuid.CleanHyphen, false) proxy := goproxy.NewProxyHttpServer() proxy.Verbose = *verbose proxy.OnRequest().HandleConnect(goproxy.AlwaysMitm) proxy.OnRequest().DoFunc(func(req *http.Request, ctx *goproxy.ProxyCtx) (*http.Request, *http.Response) { origin := ipAddrFromRemoteAddr(ctx.Req.RemoteAddr) ctx.Logf("Origin: %s", origin) /*ctx.RoundTripper = goproxy.RoundTripperFunc(func (req *http.Request, ctx *goproxy.ProxyCtx) (resp *http.Response, err error) { //data := transport.RoundTripDetails{} data, resp, err := tr.DetailedRoundTrip(req) //log.Printf("%+v", data) return })*/ rewrite := Rewrite{} if h != nil && h.Database != nil { err := h.Find(bson.M{"host": req.Host, "active": true}).One(&rewrite) if err == nil { req.URL.Scheme = rewrite.DProtocol req.URL.Host = rewrite.DHost req.Host = rewrite.DHost ctx.Logf("Rewrite: %+v, URL: %+v", rewrite, req.URL) } } //log.Printf("%+v", getHost(req.RemoteAddr)) /*if ctx.UserData != nil { from = ctx.UserData.(*transport.RoundTripDetails).TCPAddr.String() }*/ //log.Printf("Request: %s %s %s", req.Method, req.Host, req.RequestURI) //host := req.Host // Request to domain--name-co-uk.mocky.dev // will be forwarded to domain-name.co.uk /*if strings.Contains(host, ".mocky.dev") { host = strings.Replace(host, ".mocky.dev", "", 1) host = strings.Replace(host, "-", ".", -1) host = strings.Replace(host, "..", "-", -1) } else if strings.Contains(host, ".proxy.dev") { host = strings.Replace(host, ".proxy.dev", "", 1) host = strings.Replace(host, "-", ".", -1) host = strings.Replace(host, "..", "-", -1) }*/ /*r, _ := regexp.Compile(".([0-9]+)$") // Check if host is hostname.80 (host with port number) res := r.FindStringSubmatch(host) if res != nil && len(res[1]) != 0 { host = strings.Replace(host, strings.Join([]string{".", res[1]}, ""), "", 1) host = strings.Join([]string{host, res[1]}, ":") log.Printf("Changing host to %v", host); }*/ //log.Printf("Target Host: %s - Headers: %+v", host, req.Header) //req.Host = host //log.Printf("%+v", req) var reqbody []byte var bodyreader io.Reader if rules != nil && rules.Database != nil && *mock && req.Method != "CONNECT" { //reqbody := string(body[:]) //log.Printf("request body: %s", reqbody) rule := Rule{} //ctx.Logf("Looking for existing request") /*fmt.Println("RequestURI:", req.RequestURI) fmt.Println("Path:", req.URL.Path) fmt.Println("Host:", req.Host) fmt.Println("Method:", req.Method)*/ b := bson.M{"$and": []bson.M{ bson.M{"active": true}, //bson.M{"dynamic": false}, bson.M{"origin": bson.M{"$in": []interface{}{origin, false}}}, bson.M{"host": bson.M{"$in": []interface{}{req.Host, false}}}, bson.M{"method": bson.M{"$in": []interface{}{req.Method, false}}}, bson.M{"path": bson.M{"$in": []interface{}{req.URL.Path, false}}}, bson.M{"query": bson.M{"$in": []interface{}{req.URL.Query().Encode(), false}}}, }} //b := bson.M{"active": true, "dynamic": false, "host": req.Host, "method": req.Method, "path": req.URL.Path, "query": req.URL.Query().Encode()} ctx.Logf("Looking for a rule: %+v", b) err := rules.Find(b).Sort("dynamic").One(&rule) //log.Printf("Query: %+v, Res: %+v", b, rule) if err == nil { ctx.Logf("Found rule: %+v", rule) status, err := strconv.Atoi(rule.Status) if rule.Dynamic && c != nil && c.Database != nil { result := Content{} reqQuery := bson.M{"$and": []bson.M{ /*bson.M{"origin": bson.M{"$in": []interface{}{origin, false}}, },*/ bson.M{"request.host": bson.M{"$in": []interface{}{req.Host}}}, bson.M{"request.method": bson.M{"$in": []interface{}{req.Method}}}, bson.M{"request.path": bson.M{"$in": []interface{}{req.URL.Path}}}, bson.M{"response.status": bson.M{"$in": []interface{}{status}}}, /*bson.M{"query": bson.M{"$in": []interface{}{req.URL.Query().Encode()}}, },*/ }} ctx.Logf("Query %+v", reqQuery) //reqQuery := bson.M{"request.host": rule.Host, "request.method": rule.Method, "response.status": status, "request.path": rule.Path} err = c.Find(reqQuery).Sort("-date").One(&result) if err == nil && db != nil { ctx.Logf("Found a dynamic rule matching, returning it: %+v", result) respId := result.Response.FileId //reqfile, _ := getMongoFileContent(ctx, *db, result.Request.FileId) respfile, err := getMongoFileContent(ctx, *db, respId) if respfile != nil && err == nil { //reqbody := ioutil.NopCloser(bytes.NewBufferString(rule.ReqBody)) //respbody := ioutil.NopCloser(bytes.NewBufferString(rule.Body)) ctx.Logf("Header: %+v", result.Response.Headers) resp := NewResponse(req, result.Response.Headers, status, respfile) ctx.UserData = ContextUserData{Store: true, Time: 0, Body: req.Body, Header: req.Header, Origin: origin} return req, resp } else { ctx.Logf("Couldn't retrieve the response body: %+v", err) } } else { ctx.Logf("Couldn't find a dynamic response matching: %+v", err) } } else { reqbody := ioutil.NopCloser(bytes.NewBufferString(rule.ReqBody)) respbody := ioutil.NopCloser(bytes.NewBufferString(rule.Body)) ctx.Logf("Found a static rule matching, returning it: %+v", rule) resp := NewResponse(req, rule.RespHeader, status, respbody) ctx.Delay = rule.Delay ctx.UserData = ContextUserData{Store: true, Time: 0, Body: reqbody, Header: req.Header, Origin: origin} return req, resp } /*result := Content{} err = c.Find(bson.M{"_id": bson.ObjectIdHex(rule.Response)}).One(&result)*/ //err := c.Find(bson.M{"request.host": req.Host, "request.method": req.Method, "response.status": 200, "request.path": req.URL.Path}).Sort("-date").One(&result) if err == nil { //log.Printf("Found %+v", result) //respbody := result.Response.Body /*file, err := getMongoFileContent(*db, result.Response.FileId) if err == nil { resp := NewResponse(req, result.Response.Headers.Get("Content-Type"), result.Response.Status, file) ctx.UserData = ContextUserData{Store: false, Time: 0, Header: result.Request.Headers, Origin: origin} return req, resp }*/ //ctx.Logf("Found one") /*fmt.Println("Path:", result.Request.Path) //fmt.Println("Body:", result.Request.Body) fmt.Println("Method:", result.Request.Method) fmt.Println("Host:", result.Request.Host) fmt.Println("Time:", result.Request.Time) fmt.Println("Date:", result.Request.Date) fmt.Println("Headers:", result.Request.Headers) //fmt.Println("Body:", result.Response.Body) fmt.Println("Status:", result.Response.Status) fmt.Println("Headers:", result.Response.Headers)*/ //resp := goproxy.NewResponse(req, result.Response.Headers.Get("Content-Type"), result.Response.Status, result.Response.Body) //ctx.UserData = ContextUserData{Store: false, Time: 0} //return req, resp } } // read the whole body reqbody, err = ioutil.ReadAll(req.Body) if err != nil { ctx.Warnf("Cannot read request body %s", err) } defer req.Body.Close() req.Body = ioutil.NopCloser(bytes.NewBuffer(reqbody)) bodyreader = bytes.NewReader(reqbody) } else { bodyreader = req.Body } ctx.UserData = ContextUserData{Store: true, Time: time.Now().UnixNano(), Body: bodyreader, Header: req.Header, Origin: origin} return req, nil }) proxy.OnResponse().DoFunc(func(resp *http.Response, ctx *goproxy.ProxyCtx) *http.Response { //ctx.Logf("Method: %s - host: %s", ctx.Resp.Request.Method, ctx.Resp.Request.Host) if c != nil && c.Database != nil && ctx.UserData != nil && ctx.UserData.(ContextUserData).Store && ctx.Resp.Request.Method != "CONNECT" && db != nil { // get response content type respctype := getContentType(ctx.Resp.Header.Get("Content-Type")) //log.Printf("Resp Contenttype %s", respctype) respid := bson.NewObjectId() //log.Printf("Resp id: %s, host: %s", respid.Hex(), ctx.Resp.Request.Host) filename := filepath.Join(tmpdir, respid.Hex()) //log.Printf("Duplicating Body file id: %s", respid.String()) fs := NewFileStream(filename, *db, respctype, respid, ctx) reqctype := getContentType(ctx.Resp.Request.Header.Get("Content-Type")) //log.Printf("Req Contenttype %s", reqctype) if reqctype == "application/x-www-form-urlencoded" { //log.Printf("setting req content type to text/plain for saving to mongo") reqctype = "text/plain" } reqid := bson.NewObjectId() //log.Printf("Req id: %s, host: %s", reqid.Hex(), ctx.Resp.Request.Host) saveFileToMongo(*db, reqid, reqctype, ctx.UserData.(ContextUserData).Body, reqid.Hex(), ctx) // prepare document content := Content{ //Id: docid, Request: Request{ Origin: ctx.UserData.(ContextUserData).Origin, Path: ctx.Resp.Request.URL.Path, Query: ctx.Resp.Request.URL.Query().Encode(), FileId: reqid, Url: ctx.Resp.Request.URL.String(), Scheme: ctx.Resp.Request.URL.Scheme, Host: ctx.Resp.Request.Host, Method: ctx.Resp.Request.Method, Time: float32(time.Now().UnixNano()-ctx.UserData.(ContextUserData).Time) / 1.0e9, Headers: ctx.UserData.(ContextUserData).Header}, Response: Response{ Status: ctx.Resp.StatusCode, Headers: ctx.Resp.Header, FileId: respid}, SocketUUID: ctx.Uuid.Bytes(), Date: time.Now(), } err := c.Insert(content) if err != nil { ctx.Logf("Can't insert document: %v", err) } else { ctx.Logf("MongoDB document saved: %+v", content) } resp.Body = NewTeeReadCloser(resp.Body, fs) } return resp }) log.Println("Starting Proxy") log.Fatalln(http.ListenAndServe(*addr, proxy)) }
func getUuid() string { uuid.SwitchFormat("%X%X%X%X%X%X") return uuid.NewV4().String() }