|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +/// Make a call to Scarf for usage analytics. |
| 19 | +/// |
| 20 | +/// # Arguments |
| 21 | +/// |
| 22 | +/// * `language` - The language identifier (e.g., "rust", "cli") |
| 23 | +pub fn make_scarf_call(language: &str) { |
| 24 | + let language = language.to_string(); |
| 25 | + std::thread::spawn(move || { |
| 26 | + let _ = scarf_request(&language); |
| 27 | + }); |
| 28 | +} |
| 29 | + |
| 30 | +fn scarf_request(language: &str) -> Result<(), Box<dyn std::error::Error + Send + Sync>> { |
| 31 | + // Check for user opt-out |
| 32 | + if std::env::var("SCARF_NO_ANALYTICS").is_ok() || std::env::var("DO_NOT_TRACK").is_ok() { |
| 33 | + return Ok(()); |
| 34 | + } |
| 35 | + |
| 36 | + // Detect architecture and OS |
| 37 | + let arch = std::env::consts::ARCH.to_lowercase().replace(' ', "_"); |
| 38 | + let os = std::env::consts::OS.to_lowercase().replace(' ', "_"); |
| 39 | + |
| 40 | + // Construct Scarf URL |
| 41 | + let scarf_url = format!("https://sedona.gateway.scarf.sh/sedona-db/{arch}/{os}/{language}"); |
| 42 | + |
| 43 | + // Make the request using std::net::TcpStream for a simple HTTP GET |
| 44 | + if let Ok(url) = url::Url::parse(&scarf_url) { |
| 45 | + if let Some(host) = url.host_str() { |
| 46 | + let port = url.port().unwrap_or(443); |
| 47 | + let path = url.path(); |
| 48 | + |
| 49 | + // Try to make a simple HTTP request |
| 50 | + if let Ok(addr) = format!("{host}:{port}").parse::<std::net::SocketAddr>() { |
| 51 | + if let Ok(mut stream) = |
| 52 | + std::net::TcpStream::connect_timeout(&addr, std::time::Duration::from_secs(1)) |
| 53 | + { |
| 54 | + let request = |
| 55 | + format!("GET {path} HTTP/1.1\r\nHost: {host}\r\nConnection: close\r\n\r\n"); |
| 56 | + let _ = std::io::Write::write_all(&mut stream, request.as_bytes()); |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + Ok(()) |
| 63 | +} |
0 commit comments