1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
use std::collections::HashMap;
use base64::{engine::general_purpose, Engine as _};
use hmac::{Hmac, Mac};
use strum_macros::{Display, EnumString};

/// OSS配置
pub struct OSS {
    key_id: String,
    key_secret: String,
    endpoint: String,
    bucket: String,
}

pub struct RequestBuilder {
    expire: Seconds,
    parameters: HashMap<String, String>,
    content_type: Option<String>,
    content_md5: Option<String>,
    oss_headers: HashMap<String, String>,
}

impl RequestBuilder {
    pub fn new() -> Self {
        RequestBuilder {
            expire: 60,
            parameters: HashMap::new(),
            content_type: None,
            content_md5: None,
            oss_headers: HashMap::new(),
        }
    }
    pub fn expire(mut self, expire: Seconds) -> Self {
        self.expire = expire;
        self
    }
    pub fn response_content_disposition(mut self, file_name: &str) -> Self {
        self.parameters.insert("response-content-disposition".to_string(), format!("attachment;filename={}", file_name));
        self
    }
    pub fn response_content_encoding(mut self, encoding: &str) -> Self {
        self.parameters.insert("response-content-encoding".to_string(), encoding.to_string());
        self
    }
    pub fn oss_download_speed_limit(mut self, speed: i32) -> Self {
        assert!(speed >= 30, "speed must be greater than 30kb");
        self.parameters.insert("x-oss-traffic-limit".to_string(), (speed * 1024 * 8).to_string());
        self
    }
    pub fn oss_download_allow_ip(mut self, ip: &str, mask: i32) -> Self {
        self.parameters.insert("x-oss-ac-source-ip".to_string(), ip.to_string());
        self.parameters.insert("x-oss-ac-subnet-mask".to_string(), mask.to_string());
        self
    }
    pub fn oss_ac_forward_allow(mut self) -> Self {
        self.parameters.insert("x-oss-ac-forwarded-for".to_string(), "true".to_string());
        self
    }
    pub fn oss_header_put(mut self, key: &str, value: &str) -> Self {
        self.oss_headers.insert(key.to_string(), value.to_string());
        self
    }
}

type Seconds = i64;

pub trait OSSInfo {
    fn endpoint(&self) -> String;
    fn bucket(&self) -> String;
}

pub trait API {
    fn sign_url(&self, key: &str, build: RequestBuilder) -> String;
    fn key_urlencode(&self, key: &str) -> String {
        key
            .split("/")
            .map(|x| urlencoding::encode(x))
            .collect::<Vec<_>>()
            .join("/")
    }
}

pub trait OSSAPI: OSSInfo + API {
    /// 签名URL,分享下载
    /// # 使用例子
    ///
    /// ```
    /// use aliyun_oss_rust_sdk::oss::{OSS, RequestBuilder};
    /// use aliyun_oss_rust_sdk::OSSAPI;
    /// let oss = OSS::from_env();//也可以使用OSS::new()方法传递参数
    /// let build = RequestBuilder::new()
    ///    .expire(60) //60秒链接过期
    ///   .oss_download_speed_limit(30);//限速30kb
    /// let download_url = oss.sign_url_with_endpoint(
    ///     "/ipas/cn/-10/ipadump.com_imem内存修改器_1.0.0.ipa",
    ///     build
    ///     );
    ///  println!("download_url: {}", download_url);
    /// ```
    fn sign_url_with_endpoint(&self, key: &str, build: RequestBuilder) -> String {
        format!("{}.{}{}", self.bucket(), self.endpoint(), self.sign_url(key, build))
    }

    /// 签名URL,分享下载
    /// 使用自定义域名
    /// # 使用例子
    ///
    /// ```
    /// use aliyun_oss_rust_sdk::oss::{OSS, RequestBuilder};
    /// use aliyun_oss_rust_sdk::OSSAPI;
    /// let oss = OSS::from_env();//也可以使用OSS::new()方法传递参数
    /// let build = RequestBuilder::new()
    ///    .expire(60) //60秒链接过期
    ///   .oss_download_speed_limit(30);//限速30kb
    /// let download_url = oss.sign_url_with_cdn(
    ///     "https://mydomain.com",
    ///     "/ipas/cn/-10/ipadump.com_imem内存修改器_1.0.0.ipa",
    ///     build
    ///     );
    ///  println!("download_url: {}", download_url);
    /// ```
    fn sign_url_with_cdn(&self, cdn: &str, key: &str, build: RequestBuilder) -> String {
        format!("{}{}", cdn, self.sign_url(key, build))
    }
}

impl OSSAPI for OSS {}

pub trait AuthAPI {
    fn sign(
        &self,
        verb: &str,
        object: &str,
        oss_resources: &str,
        headers: &HashMap<String, String>,
        build: &RequestBuilder,
    ) -> String;
}

impl OSSInfo for OSS {
    fn endpoint(&self) -> String {
        self.endpoint.clone()
    }
    fn bucket(&self) -> String {
        self.bucket.clone()
    }
}

impl API for OSS {
    fn sign_url(&self, key: &str, build: RequestBuilder) -> String {
        let object = if key.starts_with("/") {
            key.to_string()
        } else {
            format!("/{}", key)
        };
        let mut header = HashMap::new();
        let expiration = chrono::Local::now().naive_local() + chrono::Duration::seconds(build.expire);
        header.insert("Date".to_string(), expiration.timestamp().to_string());
        let signature = self.sign(
            RequestType::Get.to_string().as_str(),
            object.as_str(),
            "",
            &header,
            &build,
        );
        let mut query_parameters = HashMap::new();
        query_parameters.insert("Expires".to_string(), expiration.timestamp().to_string());
        query_parameters.insert("OSSAccessKeyId".to_string(), self.key_id.to_string());
        query_parameters.insert("Signature".to_string(), urlencoding::encode(&signature).into_owned());
        build.parameters.iter().for_each(|(k, v)| {
            query_parameters.insert(k.to_string(), urlencoding::encode(v).into_owned());
        });

        let mut params = query_parameters
            .into_iter()
            .filter(|(k, _)| k != "x-oss-ac-source-ip")
            .collect::<Vec<_>>();

        params.sort_by(|a, b| a.0.cmp(&b.0));

        format!(
            "{}?{}",
            self.key_urlencode(key),
            params.into_iter().map(|(k, v)| format!("{}={}", k, v)).collect::<Vec<String>>().join("&")
        )
    }
}

impl<'a> AuthAPI for OSS {
    fn sign(
        &self,
        verb: &str,
        key: &str,
        oss_resources: &str,
        headers: &HashMap<String, String>,
        build: &RequestBuilder,
    ) -> String {
        let date = headers
            .get("Date")
            .map_or("", |x| x);
        let mut oss_headers = build
            .oss_headers
            .iter()
            .map(|(k, v)| (k.to_lowercase(), v))
            .collect::<Vec<_>>();

        oss_headers.sort_by(|a, b| a.0.cmp(&b.0));

        let oss_header_str = oss_headers
            .iter()
            .map(|(k, v)| format!("{}:{}", k, v))
            .collect::<Vec<_>>()
            .join("\n");

        let mut oss_resource_str = get_oss_resource_str(self.bucket.as_str(), key, oss_resources);
        if build.parameters.len() > 0 {
            let mut params = build
                .parameters
                .iter()
                .collect::<Vec<_>>();
            params.sort_by(|a, b| a.0.cmp(&b.0));
            oss_resource_str = format!(
                "{}?{}",
                oss_resource_str,
                params
                    .into_iter()
                    .map(|(k, v)| format!("{}={}", k, v))
                    .collect::<Vec<_>>()
                    .join("&")
            );
        }
        let sign_str = format!(
            "{}\n{}\n{}\n{}\n{}{}",
            verb,
            build.content_md5.clone().unwrap_or_default(),
            build.content_type.clone().unwrap_or_default(),
            date,
            oss_header_str,
            oss_resource_str,
        );
        println!("sign_str: {}", sign_str);
        let mut hasher: Hmac<sha1::Sha1> = Hmac::new_from_slice(self.key_secret.as_bytes()).unwrap();
        hasher.update(sign_str.as_bytes());

        general_purpose::STANDARD.encode(&hasher.finalize().into_bytes())
    }
}

impl<'a> OSS {
    pub fn new<S>(key_id: S, key_secret: S, endpoint: S, bucket: S) -> Self
        where
            S: Into<String>,
    {
        OSS {
            key_id: key_id.into(),
            key_secret: key_secret.into(),
            endpoint: endpoint.into(),
            bucket: bucket.into(),
        }
    }

    pub fn from_env() -> Self {
        dotenvy::dotenv().ok();
        let key_id = dotenvy::var("OSS_KEY_ID").expect("OSS_KEY_ID not found");
        let key_secret = dotenvy::var("OSS_KEY_SECRET").expect("OSS_KEY_SECRET not found");
        let endpoint = dotenvy::var("OSS_ENDPOINT").expect("OSS_ENDPOINT not found");
        let bucket = dotenvy::var("OSS_BUCKET").expect("OSS_BUCKET not found");
        OSS::new(key_id, key_secret, endpoint, bucket)
    }
}

#[inline]
fn get_oss_resource_str(bucket: &str, key: &str, oss_resources: &str) -> String {
    let oss_resources = if oss_resources != "" {
        String::from("?") + oss_resources
    } else {
        String::new()
    };
    if bucket == "" {
        format!("/{}{}", bucket, oss_resources)
    } else {
        format!("/{}{}{}", bucket, key, oss_resources)
    }
}

#[derive(EnumString, Display)]
pub enum RequestType {
    #[strum(serialize = "GET")]
    Get,
    #[strum(serialize = "PUT")]
    Put,
    #[strum(serialize = "POST")]
    Post,
    #[strum(serialize = "DELETE")]
    Delete,
    #[strum(serialize = "HEAD")]
    Head,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_sign() {
        let oss = OSS::new(
            "my_key_id",
            "my_key_secret",
            "oss-cn-shanghai.aliyuncs.com",
            "my_bucket",
        );
        let build = RequestBuilder::new()
            .expire(60)
            .oss_download_speed_limit(30);
        let download_url = oss.sign_url_with_cdn(
            "http://cdn.ipadump.com",
            "/ipas/cn/-10/ipadump.com_imem内存修改器_1.0.0.ipa",
            build,
        );
        println!("download_url: {}", download_url);
    }
}