├── .gitignore
├── LICENSE
├── README.md
└── src
├── .vs
└── config
│ └── applicationhost.config
├── FishSite.NugetWarpper.sln
└── FishSite.NugetWarpper
├── FishSite.NugetWarpper.csproj
├── Global.asax
├── Global.asax.cs
├── MetaData.cs
├── NugetCache.cs
├── Properties
└── AssemblyInfo.cs
├── Web.Debug.config
├── Web.Release.config
├── Web.config
├── index.html
└── packages.config
/.gitignore:
--------------------------------------------------------------------------------
1 | _项目发布_/
2 | _项目发布_
3 | Deploy
4 | _DB_/
5 | Packages/
6 | remote_image_cache/
7 | _bin.*
8 | *.csdat
9 | *.bin
10 | bin
11 | obj
12 | *.user
13 | *.suo
14 | *.lnk
15 | *.rar
16 | *ReSharper*
17 | *Example*
18 | Doc
19 | desktop.ini
20 | bin.*/
21 | *.sdf
22 | *.vs10x
23 | Sample Projects/
24 | .idea
25 | ipch/
26 | .vs10x
27 | *.aux
28 | *.dvi
29 | *.log
30 | *.ps
31 | *.nupkg
32 | *.sdf
33 | *.opensdf
34 |
35 | #OS junk files
36 | Thumbs.db
37 | *.DS_Store
38 | *.GhostDoc.xml
39 | #Visual Studio files
40 | *.obj
41 | *.pdb
42 | *.user
43 | *.aps
44 | *.pch
45 | *.vspscc
46 | *.vssscc
47 | *_i.c
48 | *_p.c
49 | *.ncb
50 | *.suo
51 | *.tlb
52 | *.tlh
53 | *.bak
54 | *.cache
55 | *.ilk
56 | *.log
57 | *.sbr
58 | obj/
59 | [Bb]in
60 | [Dd]ebug*/
61 | [Rr]elease*/
62 | *.min.js
63 | *.min.css
64 | #Toolinga
65 | [Tt]est[Rr]esult
66 | .pdsync
67 | .pdsyncu
68 | #Project files
69 | [Bb]uild/
70 | *.pdsync
71 | *.pdsyncu
72 | *.dotCover
73 | cache
74 | *.[Rr]elease
75 | *.[Dd]ebug
76 | /iFishWebStatic/resources/editor/xheditor/xheditor_zh-cn.min.js
77 | iFishWeb/FishSite/FishSite.UI/resources
78 | .localhistory
79 | *.min.js
80 | *.min.css
81 | *.min.js.map
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | GNU GENERAL PUBLIC LICENSE
2 | Version 2, June 1991
3 |
4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc.,
5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
6 | Everyone is permitted to copy and distribute verbatim copies
7 | of this license document, but changing it is not allowed.
8 |
9 | Preamble
10 |
11 | The licenses for most software are designed to take away your
12 | freedom to share and change it. By contrast, the GNU General Public
13 | License is intended to guarantee your freedom to share and change free
14 | software--to make sure the software is free for all its users. This
15 | General Public License applies to most of the Free Software
16 | Foundation's software and to any other program whose authors commit to
17 | using it. (Some other Free Software Foundation software is covered by
18 | the GNU Lesser General Public License instead.) You can apply it to
19 | your programs, too.
20 |
21 | When we speak of free software, we are referring to freedom, not
22 | price. Our General Public Licenses are designed to make sure that you
23 | have the freedom to distribute copies of free software (and charge for
24 | this service if you wish), that you receive source code or can get it
25 | if you want it, that you can change the software or use pieces of it
26 | in new free programs; and that you know you can do these things.
27 |
28 | To protect your rights, we need to make restrictions that forbid
29 | anyone to deny you these rights or to ask you to surrender the rights.
30 | These restrictions translate to certain responsibilities for you if you
31 | distribute copies of the software, or if you modify it.
32 |
33 | For example, if you distribute copies of such a program, whether
34 | gratis or for a fee, you must give the recipients all the rights that
35 | you have. You must make sure that they, too, receive or can get the
36 | source code. And you must show them these terms so they know their
37 | rights.
38 |
39 | We protect your rights with two steps: (1) copyright the software, and
40 | (2) offer you this license which gives you legal permission to copy,
41 | distribute and/or modify the software.
42 |
43 | Also, for each author's protection and ours, we want to make certain
44 | that everyone understands that there is no warranty for this free
45 | software. If the software is modified by someone else and passed on, we
46 | want its recipients to know that what they have is not the original, so
47 | that any problems introduced by others will not reflect on the original
48 | authors' reputations.
49 |
50 | Finally, any free program is threatened constantly by software
51 | patents. We wish to avoid the danger that redistributors of a free
52 | program will individually obtain patent licenses, in effect making the
53 | program proprietary. To prevent this, we have made it clear that any
54 | patent must be licensed for everyone's free use or not licensed at all.
55 |
56 | The precise terms and conditions for copying, distribution and
57 | modification follow.
58 |
59 | GNU GENERAL PUBLIC LICENSE
60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
61 |
62 | 0. This License applies to any program or other work which contains
63 | a notice placed by the copyright holder saying it may be distributed
64 | under the terms of this General Public License. The "Program", below,
65 | refers to any such program or work, and a "work based on the Program"
66 | means either the Program or any derivative work under copyright law:
67 | that is to say, a work containing the Program or a portion of it,
68 | either verbatim or with modifications and/or translated into another
69 | language. (Hereinafter, translation is included without limitation in
70 | the term "modification".) Each licensee is addressed as "you".
71 |
72 | Activities other than copying, distribution and modification are not
73 | covered by this License; they are outside its scope. The act of
74 | running the Program is not restricted, and the output from the Program
75 | is covered only if its contents constitute a work based on the
76 | Program (independent of having been made by running the Program).
77 | Whether that is true depends on what the Program does.
78 |
79 | 1. You may copy and distribute verbatim copies of the Program's
80 | source code as you receive it, in any medium, provided that you
81 | conspicuously and appropriately publish on each copy an appropriate
82 | copyright notice and disclaimer of warranty; keep intact all the
83 | notices that refer to this License and to the absence of any warranty;
84 | and give any other recipients of the Program a copy of this License
85 | along with the Program.
86 |
87 | You may charge a fee for the physical act of transferring a copy, and
88 | you may at your option offer warranty protection in exchange for a fee.
89 |
90 | 2. You may modify your copy or copies of the Program or any portion
91 | of it, thus forming a work based on the Program, and copy and
92 | distribute such modifications or work under the terms of Section 1
93 | above, provided that you also meet all of these conditions:
94 |
95 | a) You must cause the modified files to carry prominent notices
96 | stating that you changed the files and the date of any change.
97 |
98 | b) You must cause any work that you distribute or publish, that in
99 | whole or in part contains or is derived from the Program or any
100 | part thereof, to be licensed as a whole at no charge to all third
101 | parties under the terms of this License.
102 |
103 | c) If the modified program normally reads commands interactively
104 | when run, you must cause it, when started running for such
105 | interactive use in the most ordinary way, to print or display an
106 | announcement including an appropriate copyright notice and a
107 | notice that there is no warranty (or else, saying that you provide
108 | a warranty) and that users may redistribute the program under
109 | these conditions, and telling the user how to view a copy of this
110 | License. (Exception: if the Program itself is interactive but
111 | does not normally print such an announcement, your work based on
112 | the Program is not required to print an announcement.)
113 |
114 | These requirements apply to the modified work as a whole. If
115 | identifiable sections of that work are not derived from the Program,
116 | and can be reasonably considered independent and separate works in
117 | themselves, then this License, and its terms, do not apply to those
118 | sections when you distribute them as separate works. But when you
119 | distribute the same sections as part of a whole which is a work based
120 | on the Program, the distribution of the whole must be on the terms of
121 | this License, whose permissions for other licensees extend to the
122 | entire whole, and thus to each and every part regardless of who wrote it.
123 |
124 | Thus, it is not the intent of this section to claim rights or contest
125 | your rights to work written entirely by you; rather, the intent is to
126 | exercise the right to control the distribution of derivative or
127 | collective works based on the Program.
128 |
129 | In addition, mere aggregation of another work not based on the Program
130 | with the Program (or with a work based on the Program) on a volume of
131 | a storage or distribution medium does not bring the other work under
132 | the scope of this License.
133 |
134 | 3. You may copy and distribute the Program (or a work based on it,
135 | under Section 2) in object code or executable form under the terms of
136 | Sections 1 and 2 above provided that you also do one of the following:
137 |
138 | a) Accompany it with the complete corresponding machine-readable
139 | source code, which must be distributed under the terms of Sections
140 | 1 and 2 above on a medium customarily used for software interchange; or,
141 |
142 | b) Accompany it with a written offer, valid for at least three
143 | years, to give any third party, for a charge no more than your
144 | cost of physically performing source distribution, a complete
145 | machine-readable copy of the corresponding source code, to be
146 | distributed under the terms of Sections 1 and 2 above on a medium
147 | customarily used for software interchange; or,
148 |
149 | c) Accompany it with the information you received as to the offer
150 | to distribute corresponding source code. (This alternative is
151 | allowed only for noncommercial distribution and only if you
152 | received the program in object code or executable form with such
153 | an offer, in accord with Subsection b above.)
154 |
155 | The source code for a work means the preferred form of the work for
156 | making modifications to it. For an executable work, complete source
157 | code means all the source code for all modules it contains, plus any
158 | associated interface definition files, plus the scripts used to
159 | control compilation and installation of the executable. However, as a
160 | special exception, the source code distributed need not include
161 | anything that is normally distributed (in either source or binary
162 | form) with the major components (compiler, kernel, and so on) of the
163 | operating system on which the executable runs, unless that component
164 | itself accompanies the executable.
165 |
166 | If distribution of executable or object code is made by offering
167 | access to copy from a designated place, then offering equivalent
168 | access to copy the source code from the same place counts as
169 | distribution of the source code, even though third parties are not
170 | compelled to copy the source along with the object code.
171 |
172 | 4. You may not copy, modify, sublicense, or distribute the Program
173 | except as expressly provided under this License. Any attempt
174 | otherwise to copy, modify, sublicense or distribute the Program is
175 | void, and will automatically terminate your rights under this License.
176 | However, parties who have received copies, or rights, from you under
177 | this License will not have their licenses terminated so long as such
178 | parties remain in full compliance.
179 |
180 | 5. You are not required to accept this License, since you have not
181 | signed it. However, nothing else grants you permission to modify or
182 | distribute the Program or its derivative works. These actions are
183 | prohibited by law if you do not accept this License. Therefore, by
184 | modifying or distributing the Program (or any work based on the
185 | Program), you indicate your acceptance of this License to do so, and
186 | all its terms and conditions for copying, distributing or modifying
187 | the Program or works based on it.
188 |
189 | 6. Each time you redistribute the Program (or any work based on the
190 | Program), the recipient automatically receives a license from the
191 | original licensor to copy, distribute or modify the Program subject to
192 | these terms and conditions. You may not impose any further
193 | restrictions on the recipients' exercise of the rights granted herein.
194 | You are not responsible for enforcing compliance by third parties to
195 | this License.
196 |
197 | 7. If, as a consequence of a court judgment or allegation of patent
198 | infringement or for any other reason (not limited to patent issues),
199 | conditions are imposed on you (whether by court order, agreement or
200 | otherwise) that contradict the conditions of this License, they do not
201 | excuse you from the conditions of this License. If you cannot
202 | distribute so as to satisfy simultaneously your obligations under this
203 | License and any other pertinent obligations, then as a consequence you
204 | may not distribute the Program at all. For example, if a patent
205 | license would not permit royalty-free redistribution of the Program by
206 | all those who receive copies directly or indirectly through you, then
207 | the only way you could satisfy both it and this License would be to
208 | refrain entirely from distribution of the Program.
209 |
210 | If any portion of this section is held invalid or unenforceable under
211 | any particular circumstance, the balance of the section is intended to
212 | apply and the section as a whole is intended to apply in other
213 | circumstances.
214 |
215 | It is not the purpose of this section to induce you to infringe any
216 | patents or other property right claims or to contest validity of any
217 | such claims; this section has the sole purpose of protecting the
218 | integrity of the free software distribution system, which is
219 | implemented by public license practices. Many people have made
220 | generous contributions to the wide range of software distributed
221 | through that system in reliance on consistent application of that
222 | system; it is up to the author/donor to decide if he or she is willing
223 | to distribute software through any other system and a licensee cannot
224 | impose that choice.
225 |
226 | This section is intended to make thoroughly clear what is believed to
227 | be a consequence of the rest of this License.
228 |
229 | 8. If the distribution and/or use of the Program is restricted in
230 | certain countries either by patents or by copyrighted interfaces, the
231 | original copyright holder who places the Program under this License
232 | may add an explicit geographical distribution limitation excluding
233 | those countries, so that distribution is permitted only in or among
234 | countries not thus excluded. In such case, this License incorporates
235 | the limitation as if written in the body of this License.
236 |
237 | 9. The Free Software Foundation may publish revised and/or new versions
238 | of the General Public License from time to time. Such new versions will
239 | be similar in spirit to the present version, but may differ in detail to
240 | address new problems or concerns.
241 |
242 | Each version is given a distinguishing version number. If the Program
243 | specifies a version number of this License which applies to it and "any
244 | later version", you have the option of following the terms and conditions
245 | either of that version or of any later version published by the Free
246 | Software Foundation. If the Program does not specify a version number of
247 | this License, you may choose any version ever published by the Free Software
248 | Foundation.
249 |
250 | 10. If you wish to incorporate parts of the Program into other free
251 | programs whose distribution conditions are different, write to the author
252 | to ask for permission. For software which is copyrighted by the Free
253 | Software Foundation, write to the Free Software Foundation; we sometimes
254 | make exceptions for this. Our decision will be guided by the two goals
255 | of preserving the free status of all derivatives of our free software and
256 | of promoting the sharing and reuse of software generally.
257 |
258 | NO WARRANTY
259 |
260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
268 | REPAIR OR CORRECTION.
269 |
270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
278 | POSSIBILITY OF SUCH DAMAGES.
279 |
280 | END OF TERMS AND CONDITIONS
281 |
282 | How to Apply These Terms to Your New Programs
283 |
284 | If you develop a new program, and you want it to be of the greatest
285 | possible use to the public, the best way to achieve this is to make it
286 | free software which everyone can redistribute and change under these terms.
287 |
288 | To do so, attach the following notices to the program. It is safest
289 | to attach them to the start of each source file to most effectively
290 | convey the exclusion of warranty; and each file should have at least
291 | the "copyright" line and a pointer to where the full notice is found.
292 |
293 | {description}
294 | Copyright (C) {year} {fullname}
295 |
296 | This program is free software; you can redistribute it and/or modify
297 | it under the terms of the GNU General Public License as published by
298 | the Free Software Foundation; either version 2 of the License, or
299 | (at your option) any later version.
300 |
301 | This program is distributed in the hope that it will be useful,
302 | but WITHOUT ANY WARRANTY; without even the implied warranty of
303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
304 | GNU General Public License for more details.
305 |
306 | You should have received a copy of the GNU General Public License along
307 | with this program; if not, write to the Free Software Foundation, Inc.,
308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
309 |
310 | Also add information on how to contact you by electronic and paper mail.
311 |
312 | If the program is interactive, make it output a short notice like this
313 | when it starts in an interactive mode:
314 |
315 | Gnomovision version 69, Copyright (C) year name of author
316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
317 | This is free software, and you are welcome to redistribute it
318 | under certain conditions; type `show c' for details.
319 |
320 | The hypothetical commands `show w' and `show c' should show the appropriate
321 | parts of the General Public License. Of course, the commands you use may
322 | be called something other than `show w' and `show c'; they could even be
323 | mouse-clicks or menu items--whatever suits your program.
324 |
325 | You should also get your employer (if you work as a programmer) or your
326 | school, if any, to sign a "copyright disclaimer" for the program, if
327 | necessary. Here is a sample; alter the names:
328 |
329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program
330 | `Gnomovision' (which makes passes at compilers) written by James Hacker.
331 |
332 | {signature of Ty Coon}, 1 April 1989
333 | Ty Coon, President of Vice
334 |
335 | This General Public License does not permit incorporating your program into
336 | proprietary programs. If your program is a subroutine library, you may
337 | consider it more useful to permit linking proprietary applications with the
338 | library. If this is what you want to do, use the GNU Lesser General
339 | Public License instead of this License.
340 |
341 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Nuget本地加速缓存服务
2 |
3 | 在Visual Studio 2013、2015中,使用的是Nuget包管理器对第三方组件进行管理升级的。这个东东就类似于NodeJS中的npm。
4 | 但是很可惜的是官方的nuget服务器是国外的服务器,由于种种众所周知以及客观原因,nuget包管理器在国内使用向来很慢。
5 |
6 | ## 原理介绍&使用说明
7 |
8 | 请参见我的博客说明:[提供针对Nuget包管理器的缓存加速服务](http://blog.fishlee.net/2015/10/14/announcing_nuget_acceleration_service/)
9 |
10 | ## 项目说明
11 |
12 | 项目是一个简单的Web项目,使用了一个自定义的HttpHandler捕获所有请求,并将对应路径的文件下载并缓存。
13 | 对于索引信息和API信息文件(*.JSON),在缓存并返回前,需要替换相对应的的地址(比如把```api.nuget.org```替换为```nugetcache.nuget.org```),因此针对部分搜索请求很难提供加速效果。
14 |
15 | 同时,缓存时会保存时间戳以及相对应的数据,会按天做时间戳校验,这里会使用ETag等信息提交源服务器做校验,因此设计上可能比官方的更好(官方并没有提交这些校验,始终会要求服务器返回当前的所有信息)。
16 | 对于包文件(```*.nupkg```),不会做校验,因为Nuget官方地址不同版本号的包地址是不同的,并且Nuget目前并不允许修改或重新上传包,因此没有检测同一地址包更新的必要。
17 |
18 | ## 开发平台说明
19 |
20 | 项目使用VS2015开发,基于.NET 4.5平台。源码未针对低版本VS的编译做检测,如果发现语法或部分地方错误,除非必要,还请自行修正。
21 |
22 | 另外,由于运行方式的制约,不建议将此服务和其它站点混合使用。
23 |
24 | 最后,如果可以的话,建议直接使用我提供的服务即可,参见之前的博客说明。
25 |
26 | ## 更多计划
27 |
28 | - [ ] 引入运行统计支持
29 |
30 | ## 更新说明
31 |
32 | ###2016-08-04
33 |
34 | * 增加忽略路径功能,允许忽略符合要求的路径,以便于加入其它资源
35 |
36 | ###2016.04.16
37 |
38 | * 针对比较大的nupkg包优化,当服务器缓存需要较长时间下载时,缓存会在下载的同时向客户端发送数据
39 |
40 |
41 | ### 2016.04.06
42 |
43 | * 对nupkg包下载禁用内存缓冲模式,改为临时文件保存,降低内存使用
44 | * 支持新nuget管理器中的GZIP,对于JSON信息文件数据传输量有极大改善
45 | * 替换域名部分去除硬编码的域名
46 | * [本次更新详细说明](http://blog.fishlee.net/2016/04/07/nuget-cache-service-updated-20160406/)
--------------------------------------------------------------------------------
/src/.vs/config/applicationhost.config:
--------------------------------------------------------------------------------
1 |
2 |
20 |
21 |
22 |
23 |
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 |
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 |
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 |
322 |
323 |
324 |
325 |
326 |
327 |
328 |
329 |
330 |
331 |
332 |
333 |
334 |
335 |
336 |
337 |
338 |
339 |
340 |
341 |
342 |
343 |
344 |
345 |
346 |
347 |
348 |
349 |
350 |
351 |
352 |
353 |
354 |
355 |
356 |
357 |
358 |
359 |
360 |
361 |
362 |
363 |
364 |
365 |
366 |
367 |
368 |
369 |
370 |
371 |
372 |
373 |
374 |
375 |
376 |
377 |
378 |
379 |
380 |
381 |
382 |
383 |
384 |
385 |
386 |
387 |
388 |
389 |
390 |
391 |
392 |
393 |
394 |
395 |
396 |
397 |
398 |
399 |
400 |
401 |
402 |
403 |
404 |
405 |
406 |
407 |
408 |
409 |
410 |
411 |
412 |
413 |
414 |
415 |
416 |
417 |
418 |
419 |
420 |
421 |
422 |
423 |
424 |
425 |
426 |
427 |
428 |
429 |
430 |
431 |
432 |
433 |
434 |
435 |
436 |
437 |
438 |
439 |
440 |
441 |
442 |
443 |
444 |
445 |
446 |
447 |
448 |
449 |
450 |
451 |
452 |
453 |
454 |
455 |
456 |
457 |
458 |
459 |
460 |
461 |
462 |
463 |
464 |
465 |
466 |
467 |
468 |
469 |
470 |
471 |
472 |
473 |
474 |
475 |
476 |
477 |
478 |
479 |
480 |
481 |
482 |
483 |
484 |
485 |
486 |
487 |
488 |
489 |
490 |
491 |
492 |
493 |
494 |
495 |
496 |
497 |
498 |
499 |
500 |
501 |
502 |
503 |
504 |
505 |
506 |
507 |
508 |
509 |
510 |
511 |
512 |
513 |
514 |
515 |
516 |
517 |
518 |
519 |
520 |
521 |
522 |
523 |
524 |
525 |
526 |
527 |
528 |
529 |
530 |
531 |
532 |
533 |
534 |
535 |
536 |
537 |
538 |
539 |
540 |
541 |
542 |
543 |
544 |
545 |
546 |
547 |
548 |
549 |
550 |
551 |
552 |
553 |
554 |
555 |
556 |
557 |
558 |
559 |
560 |
561 |
562 |
563 |
564 |
565 |
566 |
567 |
568 |
569 |
570 |
571 |
572 |
573 |
574 |
575 |
576 |
577 |
578 |
579 |
580 |
581 |
582 |
583 |
584 |
585 |
586 |
587 |
588 |
589 |
590 |
591 |
592 |
593 |
594 |
595 |
596 |
597 |
598 |
599 |
600 |
601 |
602 |
603 |
604 |
605 |
606 |
607 |
608 |
609 |
610 |
611 |
612 |
613 |
614 |
615 |
616 |
617 |
618 |
619 |
620 |
621 |
622 |
623 |
624 |
625 |
626 |
627 |
628 |
629 |
630 |
631 |
632 |
633 |
634 |
635 |
636 |
637 |
638 |
639 |
640 |
641 |
642 |
643 |
644 |
645 |
646 |
647 |
648 |
649 |
650 |
651 |
652 |
653 |
654 |
655 |
656 |
657 |
658 |
659 |
660 |
661 |
662 |
663 |
664 |
665 |
666 |
667 |
668 |
669 |
670 |
671 |
672 |
673 |
674 |
675 |
676 |
677 |
678 |
679 |
680 |
681 |
682 |
683 |
684 |
685 |
686 |
687 |
688 |
689 |
690 |
691 |
692 |
693 |
694 |
695 |
696 |
697 |
698 |
699 |
700 |
701 |
702 |
703 |
704 |
705 |
706 |
707 |
708 |
709 |
710 |
711 |
712 |
713 |
714 |
715 |
716 |
717 |
718 |
719 |
720 |
721 |
722 |
723 |
724 |
725 |
726 |
727 |
728 |
729 |
730 |
731 |
732 |
733 |
734 |
735 |
736 |
737 |
738 |
739 |
740 |
741 |
742 |
743 |
744 |
745 |
746 |
747 |
748 |
749 |
750 |
751 |
752 |
753 |
754 |
755 |
756 |
757 |
758 |
759 |
760 |
761 |
762 |
763 |
764 |
765 |
766 |
767 |
768 |
769 |
770 |
771 |
772 |
773 |
774 |
775 |
776 |
777 |
778 |
779 |
780 |
781 |
782 |
783 |
784 |
785 |
786 |
787 |
788 |
789 |
790 |
791 |
792 |
793 |
794 |
795 |
796 |
797 |
798 |
799 |
800 |
801 |
802 |
803 |
804 |
805 |
806 |
807 |
808 |
809 |
810 |
811 |
812 |
813 |
814 |
815 |
816 |
817 |
818 |
819 |
820 |
821 |
822 |
823 |
824 |
825 |
826 |
827 |
828 |
829 |
830 |
831 |
832 |
833 |
834 |
835 |
836 |
837 |
838 |
839 |
840 |
841 |
842 |
843 |
844 |
845 |
846 |
847 |
848 |
849 |
850 |
851 |
852 |
853 |
854 |
855 |
856 |
857 |
858 |
859 |
860 |
861 |
862 |
863 |
864 |
865 |
866 |
867 |
868 |
869 |
870 |
871 |
872 |
873 |
874 |
875 |
876 |
877 |
878 |
879 |
880 |
881 |
882 |
883 |
884 |
885 |
886 |
887 |
888 |
889 |
890 |
891 |
892 |
895 |
896 |
897 |
898 |
899 |
900 |
901 |
902 |
903 |
904 |
905 |
906 |
907 |
908 |
911 |
912 |
913 |
914 |
915 |
916 |
917 |
918 |
919 |
920 |
921 |
922 |
923 |
924 |
925 |
926 |
927 |
928 |
929 |
930 |
931 |
932 |
933 |
934 |
935 |
936 |
937 |
938 |
939 |
940 |
941 |
942 |
943 |
944 |
945 |
946 |
947 |
948 |
949 |
950 |
951 |
952 |
953 |
954 |
955 |
956 |
957 |
958 |
959 |
960 |
961 |
962 |
963 |
964 |
965 |
966 |
967 |
968 |
969 |
970 |
971 |
972 |
973 |
974 |
975 |
976 |
977 |
978 |
979 |
980 |
981 |
982 |
983 |
984 |
985 |
986 |
987 |
988 |
989 |
990 |
991 |
992 |
993 |
994 |
995 |
996 |
997 |
998 |
999 |
1000 |
1001 |
1002 |
1003 |
1004 |
1005 |
1006 |
1007 |
1008 |
1009 |
1010 |
1011 |
1012 |
1013 |
1014 |
1015 |
1016 |
1017 |
1018 |
1019 |
1020 |
1021 |
1022 |
1023 |
1024 |
1025 |
1026 |
1027 |
1028 |
1029 |
1030 |
1031 |
1032 |
1033 |
1034 |
1035 |
1036 |
1037 |
1038 |
1039 |
--------------------------------------------------------------------------------
/src/FishSite.NugetWarpper.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio 14
4 | VisualStudioVersion = 14.0.23107.0
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FishSite.NugetWarpper", "FishSite.NugetWarpper\FishSite.NugetWarpper.csproj", "{DBAB2D9B-4D8A-4CBE-AF77-EC9ADE33D546}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|Any CPU = Debug|Any CPU
11 | Release|Any CPU = Release|Any CPU
12 | EndGlobalSection
13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
14 | {DBAB2D9B-4D8A-4CBE-AF77-EC9ADE33D546}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15 | {DBAB2D9B-4D8A-4CBE-AF77-EC9ADE33D546}.Debug|Any CPU.Build.0 = Debug|Any CPU
16 | {DBAB2D9B-4D8A-4CBE-AF77-EC9ADE33D546}.Release|Any CPU.ActiveCfg = Release|Any CPU
17 | {DBAB2D9B-4D8A-4CBE-AF77-EC9ADE33D546}.Release|Any CPU.Build.0 = Release|Any CPU
18 | EndGlobalSection
19 | GlobalSection(SolutionProperties) = preSolution
20 | HideSolutionNode = FALSE
21 | EndGlobalSection
22 | EndGlobal
23 |
--------------------------------------------------------------------------------
/src/FishSite.NugetWarpper/FishSite.NugetWarpper.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 | Debug
8 | AnyCPU
9 |
10 |
11 | 2.0
12 | {DBAB2D9B-4D8A-4CBE-AF77-EC9ADE33D546}
13 | {349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}
14 | Library
15 | Properties
16 | FishSite.NugetWarpper
17 | FishSite.NugetWarpper
18 | v4.5
19 | true
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 | true
30 | full
31 | false
32 | bin\
33 | DEBUG;TRACE
34 | prompt
35 | 4
36 |
37 |
38 | pdbonly
39 | true
40 | bin\
41 | TRACE
42 | prompt
43 | 4
44 |
45 |
46 |
47 | ..\packages\extension.fishlee.net.1.3.1.0\lib\net45\FSLib.Extension.dll
48 | True
49 |
50 |
51 | ..\packages\network.fishlee.net.2.0.0.0\lib\net45\FSLib.Network.dll
52 | True
53 |
54 |
55 | ..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.1.0.1\lib\net45\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.dll
56 | True
57 |
58 |
59 |
60 | ..\packages\Newtonsoft.Json.8.0.3\lib\net45\Newtonsoft.Json.dll
61 | True
62 |
63 |
64 | ..\packages\Owin.1.0\lib\net40\Owin.dll
65 | True
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 | Web.config
89 |
90 |
91 | Web.config
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 | Global.asax
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 |
110 | 10.0
111 | $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)
112 |
113 |
114 |
115 |
116 |
117 |
118 |
119 |
120 | True
121 | True
122 | 24806
123 | /
124 | http://localhost:24806/
125 | False
126 | False
127 |
128 |
129 | False
130 |
131 |
132 |
133 |
134 |
135 |
136 |
137 |
138 |
139 |
140 |
141 | 此项目引用这台计算机上缺少的 NuGet 程序包。使用 NuGet 程序包还原可下载这些程序包。有关详细信息,请参阅 http://go.microsoft.com/fwlink/?LinkID=322105。缺少的文件是 {0}。
142 |
143 |
144 |
145 |
146 |
153 |
--------------------------------------------------------------------------------
/src/FishSite.NugetWarpper/Global.asax:
--------------------------------------------------------------------------------
1 | <%@ Application Codebehind="Global.asax.cs" Inherits="FishSite.NugetWarpper.Global" Language="C#" %>
2 |
--------------------------------------------------------------------------------
/src/FishSite.NugetWarpper/Global.asax.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Web;
5 | using System.Web.Security;
6 | using System.Web.SessionState;
7 |
8 | namespace FishSite.NugetWarpper
9 | {
10 | using System.IO;
11 | using System.Timers;
12 | using System.Web.Hosting;
13 | using Newtonsoft.Json;
14 |
15 | public class Global : System.Web.HttpApplication
16 | {
17 |
18 | protected void Application_Start(object sender, EventArgs e)
19 | {
20 | _timer = new Timer(1000 * 60 * 20) { AutoReset = false };
21 | _timer.Elapsed += (s, ee) =>
22 | {
23 | _timer.Stop();
24 | try
25 | {
26 | SaveAllCache();
27 | }
28 | catch (Exception ex)
29 | {
30 | File.WriteAllText(Path.Combine(HostingEnvironment.ApplicationPhysicalPath, "save.txt"), ex.ToString());
31 | }
32 | _timer.Start();
33 | };
34 | _timer.Start();
35 | File.WriteAllText(Path.Combine(HostingEnvironment.ApplicationPhysicalPath, "save.txt"), "started!");
36 |
37 | }
38 |
39 | Timer _timer;
40 | static readonly object _lockObject = new object();
41 |
42 | void SaveAllCache()
43 | {
44 | lock (_lockObject)
45 | {
46 | var targetType = typeof(MetaData);
47 | var enumerator = HttpRuntime.Cache.GetEnumerator();
48 | while (enumerator.MoveNext())
49 | {
50 | if (enumerator.Value != null && enumerator.Value.GetType() == targetType)
51 | {
52 | var obj = (MetaData)enumerator.Value;
53 | File.WriteAllText(obj.CachePath, JsonConvert.SerializeObject(obj));
54 | }
55 | }
56 | File.WriteAllText(Path.Combine(HostingEnvironment.ApplicationPhysicalPath, "save.txt"), $"saved! {DateTime.Now}");
57 | }
58 | }
59 |
60 | protected void Session_Start(object sender, EventArgs e)
61 | {
62 |
63 | }
64 |
65 | protected void Application_BeginRequest(object sender, EventArgs e)
66 | {
67 |
68 | }
69 |
70 | protected void Application_AuthenticateRequest(object sender, EventArgs e)
71 | {
72 |
73 | }
74 |
75 | protected void Application_Error(object sender, EventArgs e)
76 | {
77 |
78 | }
79 |
80 | protected void Session_End(object sender, EventArgs e)
81 | {
82 |
83 | }
84 |
85 | protected void Application_End(object sender, EventArgs e)
86 | {
87 | try
88 | {
89 | SaveAllCache();
90 | }
91 | catch (Exception ex)
92 | {
93 | File.WriteAllText(Path.Combine(HostingEnvironment.ApplicationPhysicalPath, "save.txt"), ex.ToString());
94 | }
95 | }
96 | }
97 | }
--------------------------------------------------------------------------------
/src/FishSite.NugetWarpper/MetaData.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Web;
5 |
6 | namespace FishSite.NugetWarpper
7 | {
8 | using System.IO;
9 | using System.Net;
10 | using System.Text;
11 | using System.Text.RegularExpressions;
12 | using System.Threading.Tasks;
13 | using System.Web.Caching;
14 | using System.Web.Hosting;
15 | using Newtonsoft.Json;
16 |
17 | public class MetaData
18 | {
19 | [JsonIgnore]
20 | public string CachePath { get; set; }
21 |
22 | public DateTime? LastModified { get; set; }
23 |
24 | public string ETag { get; set; }
25 |
26 | public Dictionary ResponseHeaders { get; set; }
27 |
28 | public DateTime? LastUpdate { get; set; }
29 |
30 | public int HitCount { get; set; }
31 |
32 | public bool NoCheckUpdate { get; set; }
33 | }
34 | }
--------------------------------------------------------------------------------
/src/FishSite.NugetWarpper/NugetCache.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Web;
5 |
6 | namespace FishSite.NugetWarpper
7 | {
8 | using System.IO;
9 | using System.Net;
10 | using System.Text;
11 | using System.Text.RegularExpressions;
12 | using System.Threading.Tasks;
13 | using System.Web.Caching;
14 | using System.Web.Hosting;
15 | using Newtonsoft.Json;
16 |
17 | public class NugetCache : IHttpModule
18 | {
19 | private bool _enableGzip;
20 | HttpRequest request;
21 | HttpApplication context;
22 | HttpResponse response;
23 | static string _ignorePath = System.Configuration.ConfigurationManager.AppSettings["nuget_ignorepath"];
24 |
25 | ///
26 | /// 初始化模块,并使其为处理请求做好准备。
27 | ///
28 | /// 一个 ,它提供对 ASP.NET 应用程序内所有应用程序对象的公用的方法、属性和事件的访问
29 | public void Init(HttpApplication context)
30 | {
31 | context.BeginRequest += Context_BeginRequest;
32 | }
33 |
34 | private void Context_BeginRequest(object sender, EventArgs e)
35 | {
36 | context = sender as HttpApplication;
37 | request = context.Request;
38 |
39 | response = context.Response;
40 | if (!_ignorePath.IsNullOrEmpty() && Regex.IsMatch(request.Path, _ignorePath))
41 | {
42 | return;
43 | }
44 |
45 | var uri = context.Request.Url;
46 |
47 | //是否可以用GZIP?
48 | _enableGzip = (request.Headers["Accept-Encoding"] ?? "").IndexOf("gzip") != -1;
49 |
50 | try
51 | {
52 | //要求没有查询并符合规则才处理
53 | if (!uri.Query.IsNullOrEmpty() || context.Request.HttpMethod != "GET" || !Regex.IsMatch(uri.LocalPath, @"^[a-z\d/\-_\.]+\.(json|nupkg)(.*?)$", RegexOptions.IgnoreCase))
54 | {
55 | DirectProxy(context);
56 | }
57 | else
58 | {
59 | CacheProxy(context);
60 | }
61 | }
62 | catch (Exception ex)
63 | {
64 | response.StatusCode = 502;
65 | response.StatusDescription = "Gateway Error";
66 | response.Write(ex.ToString());
67 | }
68 |
69 | context.Response.End();
70 | }
71 |
72 |
73 | void CacheProxy(HttpApplication app)
74 | {
75 | var request = app.Request;
76 | var response = app.Response;
77 |
78 | var localRoot = HostingEnvironment.ApplicationPhysicalPath;
79 | var localPath = request.Url.LocalPath.Replace('/', '\\').TrimStart('\\');
80 | var localCacheFile = Path.Combine(localRoot, localPath).Replace("-gz\\", "\\");
81 | var meta = localCacheFile + ".meta";
82 | var cacheKey = "nuget_" + localPath;
83 | var responseProcessed = false;
84 |
85 | //load from cache
86 | var cache = (MetaData)HttpRuntime.Cache[cacheKey];
87 | if (cache == null)
88 | {
89 | //缓存没有,尝试加载
90 | if (File.Exists(meta))
91 | {
92 | try
93 | {
94 | cache = JsonConvert.DeserializeObject(File.ReadAllText(meta));
95 | cache.CachePath = meta;
96 | }
97 | catch (Exception)
98 | {
99 | File.Delete(meta);
100 | }
101 | }
102 | //二次判断是否有,没有则初始化一个
103 | if (cache == null)
104 | cache = new MetaData()
105 | {
106 | CachePath = meta,
107 | NoCheckUpdate = localPath.EndsWith(".nupkg", StringComparison.OrdinalIgnoreCase)
108 | };
109 | HttpRuntime.Cache.Add(cacheKey, cache, null, Cache.NoAbsoluteExpiration, new TimeSpan(0, 30, 0), CacheItemPriority.Normal, (key, value, reason) =>
110 | {
111 | var m = value as MetaData;
112 | File.WriteAllText(m.CachePath, JsonConvert.SerializeObject(m));
113 | });
114 | }
115 | //是否需要更新缓存?
116 | if (cache.LastUpdate == null || (!cache.NoCheckUpdate && cache.LastUpdate.Value.Date < DateTime.Today) || !File.Exists(localCacheFile))
117 | {
118 | if (File.Exists(meta)) File.Delete(meta);
119 | if (File.Exists(localPath)) File.Delete(localPath);
120 |
121 | //刷新缓存
122 | var webrequest = (HttpWebRequest)WebRequest.Create(new Uri(new Uri("https://api.nuget.org/"), request.Url.PathAndQuery));
123 | webrequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
124 |
125 | foreach (var header in request.Headers.AllKeys.Where(s => s.StartsWith("x-", StringComparison.OrdinalIgnoreCase)))
126 | {
127 | request.Headers.Add(header, request.Headers[header]);
128 | }
129 | if (cache.LastModified != null)
130 | request.Headers["If-Modified-Since"] = cache.LastModified.Value.ToString("R");
131 | if (!cache.ETag.IsNullOrEmpty())
132 | request.Headers["If-None-Match"] = cache.ETag;
133 |
134 | HttpWebResponse webresponse;
135 | try
136 | {
137 | webresponse = (HttpWebResponse)webrequest.GetResponse();
138 | }
139 | catch (WebException ex) when (ex.Status == WebExceptionStatus.ProtocolError)
140 | {
141 | webresponse = (HttpWebResponse)ex.Response;
142 |
143 | response.StatusCode = 502;
144 | response.StatusDescription = webresponse.StatusDescription;
145 |
146 | response.Write($"Gateway Error. The upstream server returns {response.StatusCode}");
147 |
148 | webresponse.Close();
149 | return;
150 | }
151 |
152 | cache.LastUpdate = DateTime.Now;
153 | cache.HitCount++;
154 |
155 | if (webresponse.StatusCode == HttpStatusCode.NotModified)
156 | {
157 | cache.LastModified = webresponse.GetResponseHeader("Last-Modified").ToDateTimeNullable();
158 | //没有修改过
159 | response.Headers.Add("X-FishCache", "HIT | VERIFIED");
160 | }
161 | else if (webresponse.StatusCode == HttpStatusCode.OK)
162 | {
163 | response.Headers.Add("X-FishCache", "MISS");
164 | Directory.CreateDirectory(Path.GetDirectoryName(localCacheFile));
165 |
166 | cache.ResponseHeaders = new Dictionary();
167 | foreach (var header in webresponse.Headers.AllKeys.Where(s => s.StartsWith("x-", StringComparison.OrdinalIgnoreCase)))
168 | {
169 | cache.ResponseHeaders[header] = webresponse.Headers[header];
170 | }
171 | cache.ETag = webresponse.GetResponseHeader("Etag");
172 | cache.LastModified = webresponse.GetResponseHeader("Last-Modified").ToDateTimeNullable();
173 |
174 | //copy
175 | try
176 | {
177 | if (request.Url.GetFileName().EndsWith(".json"))
178 | {
179 | var br = new StreamReader(webresponse.GetResponseStream(), Encoding.UTF8);
180 | var content = br.ReadToEnd();
181 | content = ReplaceHost(content);
182 |
183 | var buffer = Encoding.UTF8.GetBytes(content);
184 |
185 | var bufferGz = buffer.Compress();
186 | File.WriteAllBytes(localCacheFile, buffer);
187 | File.WriteAllBytes(localCacheFile + ".gz", bufferGz);
188 | }
189 | else
190 | {
191 | //临时文件
192 | var tmpfile = localCacheFile + "." + DateTime.Now.Ticks + ".tmp";
193 | var tempBuffer = new byte[0x1000];
194 | var srcStream = webresponse.GetResponseStream();
195 |
196 | //write to response at the same time
197 | foreach (var header in cache.ResponseHeaders)
198 | {
199 | response.AddHeader(header.Key, header.Value);
200 | }
201 | response.BufferOutput = false;
202 | if (webresponse.ContentLength > 0)
203 | response.AddHeader("Content-Length", webresponse.ContentLength.ToString());
204 | response.Flush();
205 |
206 | using (var tmp = File.OpenWrite(tmpfile))
207 | {
208 | var tempReadCount = 0;
209 | while ((tempReadCount = srcStream.Read(tempBuffer, 0, tempBuffer.Length)) > 0)
210 | {
211 | if (!response.IsClientConnected)
212 | {
213 | break;
214 | }
215 |
216 | tmp.Write(tempBuffer, 0, tempReadCount);
217 | response.BinaryWrite(tempReadCount == tempBuffer.Length ? tempBuffer : tempBuffer.Take(tempReadCount).ToArray());
218 | }
219 | }
220 | if (!response.IsClientConnected)
221 | {
222 | File.Delete(tmpfile);
223 | }
224 | else
225 | {
226 | File.Move(tmpfile, localCacheFile);
227 | }
228 | responseProcessed = true;
229 | }
230 | File.WriteAllText(meta, JsonConvert.SerializeObject(cache));
231 | }
232 | catch (Exception)
233 | {
234 |
235 | }
236 | }
237 |
238 | webresponse.Close();
239 | }
240 | else
241 | {
242 | response.Headers.Add("X-FishCache", "HIT");
243 | }
244 |
245 | if (responseProcessed)
246 | return;
247 |
248 | foreach (var header in cache.ResponseHeaders)
249 | {
250 | response.Headers[header.Key] = header.Value;
251 | }
252 |
253 | //写入响应
254 | if (!_enableGzip || !request.Url.GetFileName().EndsWith(".json"))
255 | {
256 | response.TransmitFile(localCacheFile);
257 | }
258 | else
259 | {
260 | response.Headers["Content-Encoding"] = "gzip";
261 | response.TransmitFile(localCacheFile + ".gz");
262 | }
263 | }
264 |
265 | string ReplaceHost(string content)
266 | {
267 | return Regex.Replace(content, @"https://api\.nuget\.org/(?=packages|v3)", "http://" + request.Url.Authority + "/");
268 | }
269 |
270 | void DirectProxy(HttpApplication context)
271 | {
272 | var request = (HttpWebRequest)WebRequest.Create(new Uri(new Uri("https://api.nuget.org/"), context.Request.Url.PathAndQuery));
273 | request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
274 | foreach (var header in context.Request.Headers.AllKeys.Where(s => s.StartsWith("x-", StringComparison.OrdinalIgnoreCase)))
275 | {
276 | request.Headers.Add(header, context.Request.Headers[header]);
277 | }
278 |
279 | var response = (HttpWebResponse)request.GetResponse();
280 | //set headers
281 | foreach (var header in response.Headers.AllKeys.Where(s => s.StartsWith("x-", StringComparison.OrdinalIgnoreCase)))
282 | {
283 | context.Response.Headers.Add(header, response.Headers[header]);
284 | }
285 | context.Response.StatusCode = (int)response.StatusCode;
286 |
287 | //copy
288 | if (context.Request.Url.GetFileName().EndsWith(".json"))
289 | {
290 | var br = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
291 | var content = br.ReadToEnd();
292 | content = ReplaceHost(content);
293 | context.Response.BinaryWrite(Encoding.UTF8.GetBytes(content));
294 | }
295 | else
296 | {
297 | response.GetResponseStream().CopyTo(context.Response.OutputStream);
298 | }
299 |
300 | response.Close();
301 | }
302 |
303 | ///
304 | /// 处置由实现 的模块使用的资源(内存除外)。
305 | ///
306 | public void Dispose()
307 | {
308 |
309 | }
310 | }
311 |
312 |
313 | }
--------------------------------------------------------------------------------
/src/FishSite.NugetWarpper/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // 有关程序集的常规信息通过下列特性集
6 | // 控制。更改这些特性值可修改
7 | // 与程序集关联的信息。
8 | [assembly: AssemblyTitle("FishSite.NugetWarpper")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("FishSite.NugetWarpper")]
13 | [assembly: AssemblyCopyright("Copyright © 2015")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // 将 ComVisible 设置为 false 会使此程序集中的类型
18 | // 对 COM 组件不可见。如果需要
19 | // 从 COM 访问此程序集中的某个类型,请针对该类型将 ComVisible 特性设置为 true。
20 | [assembly: ComVisible(false)]
21 |
22 | // 如果此项目向 COM 公开,则下列 GUID 用于 typelib 的 ID
23 | [assembly: Guid("dbab2d9b-4d8a-4cbe-af77-ec9ade33d546")]
24 |
25 | // 程序集的版本信息由下列四个值组成:
26 | //
27 | // 主版本
28 | // 次版本
29 | // 内部版本号
30 | // 修订版本
31 | //
32 | // 可以指定所有值,也可以使用“修订号”和“内部版本号”的默认值,
33 | // 方法是按如下所示使用 "*":
34 | [assembly: AssemblyVersion("1.0.0.0")]
35 | [assembly: AssemblyFileVersion("1.15288.0739.45")]
36 |
--------------------------------------------------------------------------------
/src/FishSite.NugetWarpper/Web.Debug.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
17 |
18 |
30 |
31 |
--------------------------------------------------------------------------------
/src/FishSite.NugetWarpper/Web.Release.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
17 |
18 |
19 |
31 |
32 |
--------------------------------------------------------------------------------
/src/FishSite.NugetWarpper/Web.config:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
8 |
9 |
10 |
11 |
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 |
--------------------------------------------------------------------------------
/src/FishSite.NugetWarpper/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/src/FishSite.NugetWarpper/packages.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------